Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get key from an object in an array in react-native

constructor(props) {
    super(props);
    this.state = ({
        childData: [],
        test: []
    });
}

componentDidMount() {
    let userId = firebase.auth().currentUser.uid;
    firebase.database().ref('poolog/' + userId).on('value', (snapshot) => {
       let test = [];
       snapshot.forEach((childSnapshot) => {
          let childKey = childSnapshot.key;
          test.push(snapshot.val());
        });
        this.setState({childData: test});
        // console.log(this.state.childData);
    });

I use this function in the return method of React.

 {this.state.childData.map((item, key) =>
     <View key={key}>
        {console.log(Object.values(item) + 'test')}
        <Text>{Object.keys(item)}</Text>
        {Object.values(item).map((value, index) =>
          <View></View>
        )}
     </View>
  )}

I have a question, I only want a specific value, Object.values(item) gives all the values, and when I use Object.values(item[key]) or Object.values(item[0]) I get:

TypeError: undefined is not an object (evaluating 'Object.keys(item[0]') or TypeError: undefined is not an object (evaluating 'Object.keys(item[key]')

And {console.log(Object.values(item) + 'test')} gives the following output:

[object Object],[object Object],[object Object],[object Object]test
[object Object],[object Object],[object Object],[object Object]test
[object Object],[object Object],[object Object],[object Object]test
[object Object],[object Object],[object Object],[object Object]test

How can I fix this, that I only get a specific object from this.

<Text>Object.values(item)</Text>

gives:

    10-1-201910-5-2019-10-9-2019-9-5-2019
    10-1-201910-5-2019-10-9-2019-9-5-2019
    10-1-201910-5-2019-10-9-2019-9-5-2019
    10-1-201910-5-2019-10-9-2019-9-5-2019
like image 336
JJNL77 Avatar asked Jan 26 '23 07:01

JJNL77


1 Answers

pls refer below examples...

const obj1 = {
  a: 'hello',
  b: 25,
  c: ['apple', 'mango']
};

console.log(Object.values(obj1));  // Array ['hello', 25, Array ['apple', 'mango']]
console.log(Object.keys(obj1));  // Array ['a', 'b', 'c']

so in your case,
if you use like Object.values(item) // item must be object
if you use like Object.values(item[key]) // item[key] must be object
if you use like Object.values(item[0]) // item[0] must be object

hence whatever inside Object.values() or Object.keys() must be object

so item[key] or item[0] must be evaluate to object
in your code it evaluates to undefined so only below errors are occur

TypeError: undefined is not an object (evaluating Object.keys(item[key]))
TypeError: undefined is not an object (evaluating Object.keys(item[0]))

as per your need, now you need to use like one of below:

  • Object.values(item)[0] or Object.values(item)[1] (static)
  • Object.values(item).filter((val,ind) => {}) (condition based)
  • Object.values(item).map((val,ind) => {}) (if you want to use all)

Hope this detailed explanation helps you.

like image 97
VISWANATH K Avatar answered Jan 29 '23 14:01

VISWANATH K