0

I have an object of objects.
And I need an array of data values like this

[ 'someData1', 
  'someData2', 
  'someData3' ] 

to map it to React.
I suppose that I need to use Object.keys, but I don't know the proper way to use it.
This is my code so far:

let obj = {
    0:{
        info:{
            id: 1234,
            data: 'someData1'
        },
        display:{
            raw: 'item',
            usd: 3321
        }
    },
    1:{
        info:{
            id: 2134,
            data: 'someData2'
        },
        display:{
            raw: 'item',
            usd: 3321
        }
    },
    2:{
        info:{
            id: 5478,
            data: 'someData3'
        },
        display:{
            raw: 'item',
            usd: 3321
        }
    }
};
1
  • 1
    This is a really similar question to this other one. Is this some kind of homework? Commented Dec 30, 2018 at 19:53

2 Answers 2

1

You can get an array of all the values with Object.values. Then just map() that to get the properties you want:

let obj = {0:{info:{id: 1234,data: 'someData1'},display:{raw: 'item',usd: 3321}},1:{info:{id: 2134,data: 'someData2'},display:{raw: 'item',usd: 3321}},2:{info:{id: 5478,data: 'someData3'},display:{raw: 'item',usd: 3321}}};

let data  = Object.values(obj).map(item => item.info.data)
console.log(data)

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

Comments

0

You can use for..in

let obj = { 0:{ info:{ id: 1234, data: 'someData1'},display:{raw: 'item',usd: 3321}},1:{info:{            id: 2134,data: 'someData2' }, display:{raw: 'item',           usd: 3321}}, 2:{ info:{ id: 5478, data: 'someData3'},    display:{ raw: 'item', usd: 3321 }}};

let op =[]
for (let key in obj){
  op.push( obj[key].info.data )
}
console.log(op);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.