循环遍历JavaScript中的对象并检查其属性

时间:2016-01-14 20:54:35

标签: javascript

我们的联系人列表中有一组代表不同人的对象。

为您预先编写了一个以firstName和一个属性(prop)作为参数的lookUp函数。

该函数应检查firstName是否为实际联系人的firstName,并且给定属性(prop)是该联系人的属性。

如果两者都为真,则返回"值"该财产。

如果firstName与任何联系人不对应,则返回"没有此类联系人"

如果道具与任何有效的属性不对应,则返回"没有这样的属性"

这是代码,我现在已经坚持了几个小时:(

  //Setup
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": ["Intruiging Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]
    },
];


function lookUp(firstName, prop){
// Only change code below this line
for(i = 0; i < contacts.length; i++) { 
  if(firstName == contacts.firstName && contacts.hasOwnProperty(prop)) {
    return contacts.firstName + "" + contacts[prop];
  }
  else if(firstName !== contacts.firstName) { 
    return "No such contact";
  }
  else if(contacts.hasOwnProperty(prop) === false) { 
    return "No such property";
  }
}
// Change these values to test your function
lookUp("Akira", "likes");

10 个答案:

答案 0 :(得分:2)

这可能是你想要实现的目标

function lookUp(firstName, prop){
 for (var i = 0; i < contacts.length; i++){
  if (contacts[i].firstName === firstName) {
    if (contacts[i][prop]) {
        return contacts[i][prop];
    } else {
        return "No such property";
      }
    }
  }
  return "No such contact"; 
}

答案 1 :(得分:1)

function lookUp(firstName, prop){
// Only change code below this line
var entry = contacts.find(function(d){return d.firstName === firstName;});
return entry ? ( entry.hasOwnProperty( prop ) ? entry[prop] : 'No such property' ) : 'No such contact';
}

答案 2 :(得分:1)

您可以尝试使用es6解决方案

function lookUp(firstName, prop){
  return contacts.find(x => x[prop] === firstName);
}
与其他建议相比,

看起来很小;)

祝你好运

叶戈尔

答案 3 :(得分:0)

您可以尝试过滤对象,然后检查长度和属性。不需要循环。

var contacts = [{...}...]; // just an array of objects...

function lookUp(firstName, prop) {
    var nameMatches = contacts.filter(function(contact) {
        // im using toLowerCase to make the case not a factor in finding a match
        // you may not want this, and if you don't just remove it.
        return contact.firstName.toLowerCase() === firstName.toLowerCase(); 
    });
    if (nameMatches.length === 1) {
        if (nameMatches[0].hasOwnProperty(prop)) {
            return nameMatches[0].firstName + " " + nameMatches[0][prop];
        } else {
            return "No such property";
        }
    } else {
        return "No such contact";
    }
}

答案 4 :(得分:0)

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

    }
return 'No such contact';
}

答案 5 :(得分:0)

function lookUpProfile(firstName, prop){


  for(i=0; i< contacts.length;i++) 

    {
      if ((contacts[i].firstName === firstName) && (contacts[i].hasOwnProperty(prop)===true))
        {
      return contacts[i][prop];
        }
      else if (contacts[i].hasOwnProperty(prop)===false)
        {
          return "No such property";
        }

   }
  return "No such contact";
}

这对我有用....

答案 6 :(得分:0)

本守则满足所有5个条件......

function lookUpProfile(firstName, prop){

  var a=0;
  for (var i = 0; i < contacts.length; i++) {

      if (contacts[i].hasOwnProperty(prop)) {
        
        a++;
        if (contacts[i].firstName == firstName) {
          
          return contacts[i][prop];
        }
        }
  }
     if (a===0)
     {
        return "No such property";
      }
     else{
       return "No such contact";
     }
}

答案 7 :(得分:0)

Another way to kill the cat

&#13;
&#13;
function lookUpProfile(firstName, prop){
  for (var i = 0; i < contacts.length; i++){
  if (contacts[i].firstName === firstName&& contacts[i][prop]) {
        return contacts[i][prop];
    } else if(contacts[i].firstName === firstName) {
        return "No such property";
      }}
  return "No such contact"; 
}
&#13;
&#13;
&#13;

答案 8 :(得分:-1)

您可以尝试遵循逻辑

// Gets the array of object that matches the first name
var filteredByFirstName = contacts.filter(function(item){
    return item.firstName === firstName
});

// If the length of array is 0 means no contacts
if(filteredByFirstName.length === 0) {
    return "No such contact";
} else {
   // If contact found, then check for the property
   var filteredByProperty = filteredByFirstName.filter(function(item){
      return item.hasOwnProperty(prop)
   });

   // If no property found, then return no property found message
   if(filteredByProperty.length === 0) {
      return "No such property";
   } else {
      // Otherwise return the result  
      return filteredByProperty[0].firstName + "" + filteredByProperty[0][prop];

   }
}

答案 9 :(得分:-1)

function lookUpProfile(firstName, prop) {
    for (var i=0;i<contacts.length;i++) {
        if (contacts[i].firstName===firstName) {
            if (contacts[i][prop]) {
                return contacts[i][prop];
            }
            else {
                return "No such property";
            }          
        }             
    }
  return "No such contact";
}