The returned property of an object, if it is in 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)that is for input:

lookUpProfile("Akira", "likes");

will return:

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

This is my solution so far, which still does not return:

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

Anyone have an idea to solve this? Thanks

+6
source share
4 answers

The problem is that you are trying to iterate over an object with a for loop with numeric divisions. You don’t need a second loop at all. It can 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"))
Run codeHide result
+1
source

. .find() (ES6) , [].

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);
Hide result
+3

:

for(var j = 0; j < contacts[i].length; j++){

. , [i] , , length, 0 < undefined, .

. , prop . , , . , [prop] .

, , .

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];
  }
 }
}
+1

:

// Create a function which will filter out the requirement
var filtered = function(name, likes) {
  var _toRet = ''
  contacts.filter(function(elem, index) {
    // check if the first name is same as requires
    // check if the object have property likes
    // Since the requirement is to search the likes array
    if (elem.firstName === name && elem.hasOwnProperty(likes)) {
      _toRet = elem[likes]  
    }
    return _toRet  // return from filter callback

  })
return _toRet  // return from the function
}

console.log(filtered('Akira', 'likes'))

DEMO

+1

Source: https://habr.com/ru/post/1015822/


All Articles