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 name from the json structure described above? Thank you

4

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

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest