I am getting a response of an api request in JSON format which I store in a variable with json data like this structure
const users= {
'ABC': {
id: '123',
name:'testname',
},
'DEF': {
id = '222',
name='testname2'
}
}
I am trying to read each data under users and store in a different variable. One variable lets say name firstVar which will have all date under 'ABC' and secondVar which will have all data under'DEF' which i can then use for further tests. I am not sure how to achieve it. Any help will be appreciable.
If you want to get the ids of each object and store them you can map
over the Object.values
and return an array of ids.
const users = {
'ABC': {
id: '123'
},
'DEF': {
id: '222'
}
};
const ids = Object.values(users).map(el => {
return el.id;
});
console.log(ids);
Not sure if this is what you want, your code is cut off. Array.prototype.find function only works for Array.
const users = {
'ABC': {
id: '123'
},
'DEF': {
id: '222'
}
};
const foundId = Object.keys(users).find(username => users[username].id === '123');
users[foundId] // found user.