0

There is a JSON file containing objects I import to my script. The file looks this way, more or less:

{
"names": [
{
    "name": "latacz",
    "displayName": "latacz"
},
{
    "name": "bomkliwer",
    "displayName": "bomkliwer"
},
(...)

It gets parsed correctly, values are stored in an object named sail, and when I console.log(sail) the contents of the object, I get this:

enter image description here

My question is: how can I get the values, so the displayNames and names of the array elements?

2
  • 1
    Thank you for not calling it a JSON array :). Commented Jan 20, 2017 at 20:25
  • Doing my best ;-) Commented Jan 20, 2017 at 20:29

4 Answers 4

1
var names = sail.names;

names.forEach(function(item){
    var name = item.name;
    // Console.log("name: ", name);
    var displayName = item.displayName;
    // Console.log("displayName: ", displayName);
})
Sign up to request clarification or add additional context in comments.

Comments

1
function getData(sail){
   sail.forEach(function(item){
      console.log('name:', item.name);
      console.log('displayName:', item.displayName);
  });
}

Comments

1

What about sail.names[0].name and sail.names[0].displayName?

However, I don't understand what you need the name sail for, if the array already has its name names. I would decide for one.

Comments

1

Your Object has an array element named "names". You should navigate for this array.

var a = "Your Object";
for(var i of a.names){
  console.log(i);
  console.log(i.name);
  console.log(i.display);
}

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.