i got object like this.
Object {
"company": "984",
"id": "1",
}
i want to access company and id individually.
The result should look like this
"984"
"1"
How can i achieve these?Thanks.
CodePudding user response:
You can write the following function:
let obj = {
"company": "984",
"id": "1",
}
let printObject = function(obj) {
let props = Object.getOwnPropertyNames(obj)
props.forEach(el => {
console.log('' el '--->', obj[el]);
});
}
printObject(obj);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Object.values
will give you an array of the values
const obj = {
"company": "984",
"id": "1",
};
console.log(Object.values(obj));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
You can of course assign this array to a variable and then access the values individually
const vals = Object.values(obj);
console.log(vals[0]); /// "984"
console.log(vals[1]); /// "1"