简体   繁体   English

遍历对象属性/键

[英]Iterating through object properties/keys

I'm just starting to learn coding, and i came across this question that i could not understand. 我刚刚开始学习编码,遇到了一个我无法理解的问题。

"The second function we'll add will be called search, and it will take a first name as an argument. It will try to match the first name it receives to any of the first names in our friends contact list. If it finds a match, it will log our friend's contact information (firstName, lastName, number, address) to the console." “我们将添加的第二个功能称为搜索,它将以名字作为参数。它将尝试将收到的名字与我们的朋友联系列表中的任何名字匹配。如果找到一个匹配,它将把我们朋友的联系信息(名字,姓氏,号码,地址)记录到控制台。”

variables are define as follows : 变量定义如下:

var friends = {}; 
friends.bill = { 
  firstName: "Bill", 
  lastName: "gates", 
  number: "1234567", 
  address: ['bishan','starbucks', 'centertable'] 
}; 

friends.steve = { 
  firstName: "Steve", 
  lastName: "jobs", 
  number: "987654", 
  address: ['orchird', 'ikoma', 'ga'] 
};

the answer is as follows : 答案如下:

var search = function(name) {
    for(var key in friends) {
        if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        }
    }
};

could someone better explain how did the var "key" came about ? 有人可以更好地解释var“密钥”是如何产生的吗? and why can't i just input friends.firstName === name, console.log(friends.name), return friends.name ?? 为什么我不能只输入friends.firstName === name,console.log(friends.name),return friends.name?

would appreciate if someone could explain thank you. 如果有人可以解释谢谢您,我将不胜感激。

From OP's comment: 从OP的评论:

var friends = {}; 

friends.bill = { 
  firstName: "Bill", 
  lastName: "gates", 
  number: "1234567", 
  address: ['bishan','starbucks', 'centertable'] 
}; 

friends.steve = { 
  firstName: "Steve", 
  lastName: "jobs", 
  number: "987654", 
  address: ['orchird', 'ikoma', 'ga'] 
};

friends is a nested object which can also be represented like so: friends是一个嵌套对象,也可以这样表示:

friends = {
  bill: { 
      firstName: "Bill", 
      lastName: "gates", 
      number: "1234567", 
      address: ['bishan','starbucks', 'centertable'] 
  },
  steve: { 
      firstName: "Steve", 
      lastName: "jobs", 
      number: "987654", 
      address: ['orchird', 'ikoma', 'ga'] 
  }
}

The for..in loop iterates over all keys in the friends object, with the variable key in your case. for..in循环遍历friends对象中的所有键,并以您的情况为变量key

why can't i just input friends.firstName === name, console.log(friends.name), return friends.name ?? 为什么我不能只输入friends.firstName === name,console.log(friends.name),返回friends.name?

Because, to do that, you need to have firstName or name as a property in friends . 因为要做到这一点,您需要将firstNamename作为属性的friends Since those properties are nested inside ( name is not event inside the nested objects), there was a for..in loop used. 由于这些属性嵌套在内部( name不是嵌套对象内部的事件),因此使用了for..in循环。

You have an object friends that has 2 properties bill and steve (those are the keys). 您有一个对象friends ,该friends具有2个属性billsteve (这些是键)。 Calling friends.bill will return you an object (the value) with firstname, lastname, number, address . 调用friends.bill会返回一个具有firstname, lastname, number, address的对象(值)。 You need to iterate all the properties of your object friends to find the one you need 您需要迭代对象friends所有属性以找到所需的属性

You can use Object.values(obj) 您可以使用Object.values(obj)

 var firstNameInput = "Steve"; var friends = {}; friends.bill = { firstName: "Bill", lastName: "gates", number: "1234567", address: ['bishan','starbucks', 'centertable'] }; friends.steve = { firstName: "Steve", lastName: "jobs", number: "987654", address: ['orchird', 'ikoma', 'ga'] }; //Iterates all the friends Object.values(friends).forEach(function(f){ //Compare the property "firstname" with the input if(f.firstName === firstNameInput){ //Friend found console.log(f); return; } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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