简体   繁体   中英

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

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.

You can get the keys of a particular object with object.keys

The Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]

I recommend you use lodash: https://lodash.com/docs/4.17.11#get

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

_.get(obj1, 'a');
// => 'hello'

_.get(obj1, 'c[0]');
// => 'apple'

_.get(obj1, 'a.b.c', 'default');
// => 'default'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM