I have fetched data from an business api and trying to assign the company names to one variable. For sake of simplicity I have created an object below to show what I'm trying to access. I am trying to assign all of the object_name into one variable and access its properties in a multi dimensional array. But it seems to be assigning the entire object when I just want all the object_name.
const object = [
{object_name: 'item1', employees: 1, net_worth: 100},
{object_name: 'item2', employees: 2, net_worth: 200},
{object_name: 'item3', employees: 3, net_worth: 300},
{object_name: 'item4', employees: 4, net_worth: 400},
]
const names = object.object_name
console.log(names) // Just prints out all of the object when I just want the object_name
expected 'item1', 'item2','item3','item4',
CodePudding user response:
const object = [
{object_name: 'item1', employees: 1, net_worth: 100},
{object_name: 'item2', employees: 2, net_worth: 200},
{object_name: 'item3', employees: 3, net_worth: 300},
{object_name: 'item4', employees: 4, net_worth: 400},
]
const names = object.map(o => o.object_name)
console.log(names)