I am calling an API and got a response an an object in the following form. I would like to append the objects to an array in order to iterate them later.
Using push I got a list of strings == ['Account','AccountName','AccountServiceHomepage']
I would like to push the entire object to the array so for x[0] I get Account as an object not as a string.
Does anyone have a clue?
Snippet
let properties = {
Account: {
label: 'Account',
key: 'Account',
description: { en: '' },
prefLabel: { en: 'test' },
usageCount: '0'
},
AccountName: {
label: 'AccountName',
key: 'AccountName',
description: { en: '' },
prefLabel: { en: '' },
usageCount: '0'
},
AccountServiceHomepage: {
label: 'AccountServiceHomepage',
key: 'AccountServiceHomepage',
description: { en: '' },
prefLabel: { en: '' },
usageCount: '0'
}
}
x = [];
for (i in properties) {
x.push(i);
};
console.log(x);
CodePudding user response:
let x = Object.values(properties)
CodePudding user response:
let properties = {
Account: {
label: 'Account',
key: 'Account',
description: { en: '' },
prefLabel: { en: 'test' },
usageCount: '0'
},
AccountName: {
label: 'AccountName',
key: 'AccountName',
description: { en: '' },
prefLabel: { en: '' },
usageCount: '0'
},
AccountServiceHomepage: {
label: 'AccountServiceHomepage',
key: 'AccountServiceHomepage',
description: { en: '' },
prefLabel: { en: '' },
usageCount: '0'
}
}
x = [];
for (i in properties) {
x.push(properties[i]);
};
console.log(x);
CodePudding user response:
You can convert object to an array (ignoring keys) like this:
const items = Object.values();
CodePudding user response:
Try this:
for (let property in properties) {
propertiesArray.push(properties[property]);
}