Get Value from Array of Object
I Am Having Trouble Accessing Elements of an Array. I Want to Assign Project Name to Local Variable projectName Can Anyone Help Me Solve How to Get the Project...
I am having trouble accessing elements of an array. I want to assign project name to local variable projectName can anyone help me solve how to get the project name from the json structure described above? Thank you
2 Answers
You can use array map and return.
let data = [{...}, {...}];
let projectNames = data.map(item => {
return item.project.name;
});
Your return value will be an array of strings
["Example123", "test"]
Assuming that you JSON is something like this:
data = [
{
project: {
coverUrl: null,
description: null,
id: 'some-guid-1',
name: 'Example123'
}
},
{
project: {
coverUrl: null,
description: null,
id: 'some-guid-2',
name: 'test'
}
}
]
Then accessing and assigning a project name would require the position, plus the properties:
var projectName = data[0].project.name // 'Example123'