javascript 获取字典的键值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19299033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get key value of dictionary
提问by user984003
How do I get the value from a javascript dictionary? (I don't even know if it's called a dictionary in javascript)
如何从 javascript 字典中获取值?(我什至不知道它在javascript中是否称为字典)
I get the following object (friends) from the facebook sdk. How do I, for example, loop through the names?
我从 facebook sdk 得到以下对象(朋友)。例如,我如何遍历名称?
{data: [{id: "xxxxx", name: "Friend name"}, {id: "xxxxx", name: "Friend name"}]}
回答by VisioN
In JavaScript dictionaries are objects. To access object properties you may use either dot notation or square brackets notation. To iterate an array you may use simple forloop.
在 JavaScript 中,字典是对象。要访问对象属性,您可以使用点符号或方括号符号。要迭代数组,您可以使用简单的for循环。
var obj = {
data: [{
id: "xxxxx",
name: "Friend name"
}, {
id: "xxxxx",
name: "Friend name"
}]
};
for (var i = 0, len = obj.data.length; i < len; i++) {
console.log(obj.data[i].name);
}
回答by Andy
Loop through the data array within the object that wraps the whole thing. Then target the name with object dot notation:
循环遍历包装整个事物的对象内的数据数组。然后使用对象点符号定位名称:
for (var i = 0, l = obj.data.length; i < l; i++) {
console.log(obj.data[i].name);
}
回答by Vlad Bezden
回答by Naftali aka Neal
You can loop through the dataarray like this:
您可以data像这样循环遍历数组:
var obj = {data: [{id: "xxxxx", name: "Friend name"}, {id: "xxxxx", name: "Friend name"}]};
//loop thru objects in data
obj.data.forEach(function(itm) {
console.log(itm.name);
});

