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

82 thoughts on “Get key from an object in an array in react-native”

Leave a Comment