简体   繁体   中英

How to get values from array created based on JSON input?

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:

在此处输入图片说明

My question is: how can I get the values, so the displayName s and name s of the array elements?

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.

var names = sail.names;

names.forEach(function(item){
    var name = item.name;
    // Console.log("name: ", name);
    var displayName = item.displayName;
    // Console.log("displayName: ", displayName);
})
function getData(sail){
   sail.forEach(function(item){
      console.log('name:', item.name);
      console.log('displayName:', item.displayName);
  });
}

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); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM