4
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
3
  • You can fix the [object Object] issue with JSON.stringify. Commented May 16, 2019 at 10:22
  • Object.values(myObject) will give you an array of all the values in that object. for instance: Object.values({ one: 1, two: 2 })[1] === 2 Commented May 16, 2019 at 10:47
  • @JJNL77 can show the json structure you are working with ? Commented May 16, 2019 at 10:51

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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"]

Comments

1

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.