Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return object's property if it is found into an array of objects in JavaScript

I have the following array of objects:

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"]
    }
];

I want to define a function, lookUpProfile(firstName, prop), which for the input data:

lookUpProfile("Akira", "likes");

will return:

["Pizza", "Coding", "Brownie Points"]

This is my solution so far which doesn't return something till now:

function lookUpProfile(firstName, prop){
    for(var i = 0; i < contacts.length; i++){
        if(contacts[i].firstName == firstName){
            for(var j = 0; j < contacts[i].length; j++){
                if(contacts[i].hasOwnProperty(prop))
                    return prop;
            }
        }
    }
}

Has anyone any idea to solve this? Thanks

like image 397
Samurai Jack Avatar asked Mar 17 '17 10:03

Samurai Jack


2 Answers

You can try to do like this. Find the item with .find() function (ES6) and return the appropriate property using bracket [] syntax.

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 lookUpProfileES6(name, prop){
  var found = contacts.find(item => item.firstName === name);
  if(found){
      return found[prop];
  }
}

function lookUpProfileES5(name, prop){
  var found = null;
  for(var i = 0; i < contacts.length; i++){
      if(contacts[i].firstName === name){
         found = contacts[i];
         break;
      }
  }
  
  if(found){
      return found[prop];
  }
}

var resultES6 = lookUpProfileES6("Akira", "likes");
var resultES5 = lookUpProfileES5("Akira", "likes");

console.log(resultES6);
console.log(resultES5);
like image 74
Suren Srapyan Avatar answered Oct 15 '22 00:10

Suren Srapyan


You problem is that you are trying to loop over object with for loop with numeric indeces. You don't need second loop at all. It could be as simple as

function lookUpProfile(firstName, prop) {
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName) {
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
      }
    }
  }
}

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 lookUpProfile(firstName, prop) {
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName) {
      if (contacts[i].hasOwnProperty(prop))
        return contacts[i][prop];
    }
  }
}

console.log(lookUpProfile("Akira", "likes"))
like image 40
dfsq Avatar answered Oct 14 '22 22:10

dfsq