0

How can I get the values of Array which Nested in a JSON values and the JSON also nested in a Array?

var contacts = [
  {
    "firstName": "Akira",
    "lastName": "Laine",
    "number": "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"]
  },
  {
    "firstName": "Harry",
    "lastName": "Potter",
    "number": "0994372684",
    "likes": ["Hogwarts", "Magic", "Hagrid"]
  },
  {
    "firstName": "Sherlock",
    "lastName": "Holmes",
    "number": "0487345643",
    "likes": ["Intriguing Cases", "Violin"]
  },
  {
    "firstName": "Kristian",
    "lastName": "Vos",
    "number": "unknown",
    "likes": ["Javascript", "Gaming", "Foxes"]
  }
];


var firstName = "Akira", prop = "likes";

for (var i = 0; i < contacts.length; i++) {
  var a;
  console.log("contacts[i].firstName = " + contacts[i].firstName);
  if(contacts[i].firstName == firstName){
    a = true;
  } else {
    a = false;
  }
  // get the values of likes?
  // console.log("contacts[i].prop[i].value() = " + contacts[i].prop[i].value() );
  console.log("contacts[i][prop].value() = " + contacts[i][prop].value() );
}

if I want to get the value of likes & "firstName": "Akira", what should I do? Anybody can help?

solution

var contacts = [
  {
    "firstName": "Akira",
    "lastName": "Laine",
    "number": "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"]
  },
  {
    "firstName": "Harry",
    "lastName": "Potter",
    "number": "0994372684",
    "likes": ["Hogwarts", "Magic", "Hagrid"]
  },
  {
    "firstName": "Sherlock",
    "lastName": "Holmes",
    "number": "0487345643",
    "likes": ["Intriguing Cases", "Violin"]
  },
  {
    "firstName": "Kristian",
    "lastName": "Vos",
    "number": "unknown",
    "likes": ["Javascript", "Gaming", "Foxes"]
  }
];


function getValuesByProps(firstName = "Akira", prop = "likes") {
  var result;
  for (var i = 0; i < contacts.length; i++) {
    if(contacts[i].firstName === firstName){
      console.log("contacts[i][prop] = " + contacts[i][prop]);
      result = contacts[i][prop];
      break;
    } 
  }
  return result;
}

xgqfrms
  • 10,077
  • 1
  • 69
  • 68
  • 1
    What is the issue? Your code seems ok. Where you have `a = true`, you can just go `contacts[i].likes` or `contacts[i][prop]` to get the array. – Shilly Sep 26 '16 at 15:12
  • https://github.com/gildata/RAIO/issues/61 –  Aug 10 '17 at 06:57

4 Answers4

0

The objects in the contacts array do not have a property whose name is prop. Fortunately, you can access properties on an object by name similar to how you would get a value out of an array, so if you use contacts[i][prop] it will get you the value of the property whose name is the value of the variable prop, which in this case is "likes". Since likes is also an array, you would then get values out of it by indexing the same way you are indexing the contacts array.

var firstName = "Akira", prop = "likes";
for (var i = 0; i < contacts.length; i++) {
    var a;
    console.log("contacts[" + i + "].firstName = " + contacts[i].firstName);
    if (contacts[i].firstName == firstName){
        a = true;
    } else {
        a = false;
    }

    // get the likes array and loop through to print the values
    // you might want to put this where a = true; is if you only want to get 
    // the likes where the firstName matches
    var likes = contacts[i][prop];
    for (var j = 0; j < likes.length; j++) {
        console.log("contacts[" + i + "]." + prop + "[" + j + "] = " + likes[j]);
    }
}
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
0

Yoo might do as follows;

var contacts = [
        {
            "firstName": "Akira",
            "lastName": "Laine",
            "number": "0543236543",
            "likes": ["Pizza", "Coding", "Brownie Points"]
        },
        {
            "firstName": "Harry",
            "lastName": "Potter",
            "number": "0994372684",
            "likes": ["Hogwarts", "Magic", "Hagrid"]
        },
        {
            "firstName": "Sherlock",
            "lastName": "Holmes",
            "number": "0487345643",
            "likes": ["Intriguing Cases", "Violin"]
        },
        {
            "firstName": "Kristian",
            "lastName": "Vos",
            "number": "unknown",
            "likes": ["Javascript", "Gaming", "Foxes"]
        }
    ],
  getMeObj = (d,p,v,t) => d.filter(o => o[p] === v)[0][t];
  console.log(getMeObj(contacts,"firstName", "Akira", "likes"));
Redu
  • 25,060
  • 6
  • 56
  • 76
0

The following code will work

/*

var firstName = "Akira", prop = "likes";

for(var i in contacts) {
  if(contacts[i].firstName === firstName){
    var likes = contacts[i][prop];
    console.log(likes);
  }
}

*/

let firstName = "Akira", prop = "likes";

for(let item of contacts) {
  if(item.firstName === firstName){
    const likes = item[prop];
    console.log(likes);
  }
}
xgqfrms
  • 10,077
  • 1
  • 69
  • 68
VelNaga
  • 3,593
  • 6
  • 48
  • 82
0

I have fixed your code. if you need more details ask me but I think the code is simple enough

var contacts = [{
  "firstName": "Akira",
  "lastName": "Laine",
  "number": "0543236543",
  "likes": ["Pizza", "Coding", "Brownie Points"]
}, {
  "firstName": "Harry",
  "lastName": "Potter",
  "number": "0994372684",
  "likes": ["Hogwarts", "Magic", "Hagrid"]
}, {
  "firstName": "Sherlock",
  "lastName": "Holmes",
  "number": "0487345643",
  "likes": ["Intriguing Cases", "Violin"]
}, {
  "firstName": "Kristian",
  "lastName": "Vos",
  "number": "unknown",
  "likes": ["Javascript", "Gaming", "Foxes"]
}];
var firstName = "Akira"
  , prop = "likes";
for (var i = 0; i < contacts.length; i++) {
  var a;
  console.log("contacts[" + i + "].firstName = " + contacts[i].firstName);
  if (contacts[i].firstName == firstName) {
    a = true;
  } else {
    a = false;
  }
  for (var n = 0; n < contacts[i].likes.length; n++) {
    console.log("contacts[" + i + "].likes[" + n + "] = " + contacts[i].likes[n]);
  }
}

As a side note:answers should be in the same level of the asker to help with learning :)

  • The question appears to be asking how to get a property when all you have is a variable with the name of the property in it, in this case `var prop = "likes"`. This answer is getting the `likes` property directly without using the variable `prop`, so I'm not sure if it actually answers the question. (just an assumption that I and several others have made. It could very well be that you are the one who answered what is actually being asked) – jonhopkins Sep 26 '16 at 15:34