简体   繁体   English

遍历对象并返回所有属性数组的值

[英]loop through object and return value of all property arrays

I have a javascript object with a bunch of properties. 我有一个带有一堆属性的javascript对象。 In this example each property represents a piece of clothing, with a key and then a value that is an array. 在此示例中,每个属性代表一件衣服,其中包含一个键,然后是一个数组值。

var clothes = {
  CLO1: ["shirt", "cotton", "white"],
  CLO2: ["tie", "silk", "red"],
  CLO3: ["shoes", "leather", "black"]
};

I want to loop through each one and print out the color for each piece of clothing. 我想遍历每件衣服,并打印出每件衣服的颜色。 I'm trying to figure out the most concise way to do this. 我正在尝试找出最简洁的方法。 Something like this-- 像这样的东西

for (property in object) {
if (property[key]){
    return (property[2])
  } else { return "none";
}

Any thoughts would be really greatly appreciated. 任何想法将不胜感激。 I hope this makes sense. 我希望这是有道理的。 Thanks! 谢谢!

Looks fine to me. 在我看来很好。 Are arrays sometimes empty or null? 数组有时为空还是为空? The suggestion in the comments to remove the extra return statement is a good suggestion. 注释中建议删除多余的return语句是一个很好的建议。

I noticed that each array has a similar structure. 我注意到每个数组都有类似的结构。 The first element is the type of clothing, the second element is the material and the third element is the color. 第一个元素是衣服的类型,第二个元素是材料,第三个元素是颜色。 You can use an object instead of an array. 您可以使用对象而不是数组。 The advantage of using an object is that it tells you (and other programmers) more about the structure of your data. 使用对象的好处是它可以告诉您(和其他程序员)更多有关数据结构的信息。 Use an array to store a collection of these objects: 使用数组存储这些对象的集合:

var clothes = [
  { type: "shirt",
    material: "cotton",
    color: "white"
  },
  { type: "belt",
    material: "leather",
    color: "none"
];

Also, instead of checking if the property "color" exists, always include "color". 另外,不要检查属性“ color”是否存在,而应始终包括“ color”。 Set "color" to "none" if it is not relevant. 如果不相关,请将“颜色”设置为“无”。

Printing out the colors looks like: 打印出颜色看起来像:

clothes.forEach(function(each) {
    console.log(each.color);
});

UPDATE I chose to always include "color" because it simplifies the procedural code. 我选择始终包含“颜色”的UPDATE,因为它简化了过程代码。 Without the data redundancy I must check to see if particular keys exist when I iterate over the properties. 没有数据冗余,我必须检查迭代属性时是否存在特定的键。 I usually choose to simplify the code, not the data. 我通常选择简化代码,而不是数据。 Philosophically, it is similar the trade-offs between dense and sparse data representations. 从哲学上讲,这类似于密集和稀疏数据表示之间的权衡。

There is also a semantic reason always including color. 还有一个语义原因,总是包含颜色。 What if some clothes have the property "size", but others don't. 如果有些衣服具有“大小”属性,而其他衣服没有,该怎么办。 What if all the examples I look at do not have "size"? 如果我查看的所有示例都没有“大小”怎么办? I would not know to include "size" in my procedural code. 我不知道在程序代码中包含“大小”。

var clothes = {
  CLO1: ["shirt", "cotton", "white"],
  CLO2: ["tie", "silk", "red"],
  CLO3: ["shoes", "leather", "black"]
};

var print = function(obj) {
    for (var property in obj) {
        if (obj.hasOwnProperty(property)) {
            console.log(obj[property][2]);
        }
    }
}   

print(clothes);

On each iteration of the for loop, the property variable holds the name of the property, you then have to have to index into obj to retrieve the value of the property, and then again index the second element of the array to get the colour you asked for. 在for循环的每次迭代中,属性变量保存属性的名称,然后必须索引到obj以检索属性的值,然后再次索引数组的第二个元素以获取颜色要求。

This works, although I would suggest creating an structure (as suggested by @ahoffer) to hold these items. 尽管我建议创建一个结构(如@ahoffer所建议的)来容纳这些项目,但这是可行的。

// - Object.keys takes an object and returns an array of keys
// - forEach iterates that array
// - console.log prints it to the console

Object.keys(clothes).forEach(function (type) {
    console.log(clothes[type][2]);
});

Maybe something like that? 也许是这样吗?

Object.keys(clothes).forEach(function(type) {
   console.log(type + ": " + clothes[type].join(', '));
});

The code above will give you this: 上面的代码将为您提供:

CLO1: shirt, cotton, white
CLO2: tie, silk, red
CLO3: shoes, leather, black

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

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