2

enter image description here

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
  • 1
    Share your code with us and if possible, create a MVCE. Commented Jul 29, 2020 at 14:04
  • 1
    Assuming the array is called input: input[0].project.name for 'Example123', input[1].project.name for 'test' and input.map(item => item.project.name) for ['Example123', 'test']. Commented Jul 29, 2020 at 14:11
  • Adding the actual JSON instead of a screenshot would be more helpful. Commented Jul 29, 2020 at 14:59
  • thank you for the help and next time i will make sure to add json or code instead screenshot. Commented Jul 30, 2020 at 6:43

2 Answers 2

5

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"]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Joe Tan mapping data works perfect for my case.
0

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'

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.