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"]
1 Comment
tiana
Thank you @Joe Tan mapping data works perfect for my case.
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'

input:input[0].project.namefor'Example123',input[1].project.namefor'test'andinput.map(item => item.project.name)for['Example123', 'test'].