如果我有这样的数组:
var arrayData = [
{"id" : 1, "title" : "a title", "info" : "blah blah"},
{"id" : 2, "title" : "another title", "info" : "lalala"},
...
];
我如何遍历数组并找到title
的值?
for (var i=0; i < entries.length; i++) {
// console.log('title of this post is:')
}
答案 0 :(得分:2)
for (var i=0; i < entries.length; i++) {
console.log(entries[i].title);
}
答案 1 :(得分:2)
如果您可以使用ES5,您可以获得如下标题数组:
arrayData.map( function(record) { return record.title; });
答案 2 :(得分:1)
数组中的每个对象都可以通过其索引访问,例如arrayData[0]
,并且可以以常规方式访问该对象的属性,例如arrayData[0].title
,因此您的循环只需要使用arrayName和i:
var arrayData = [
{"id" : 1, "title" : "a title", "info" : "blah blah"},
{"id" : 2, "title" : "another title", "info" : "lalala"}
];
for (var i=0; i < arrayData.length; i++) {
console.log('title of this post is: ' + arrayData[i].title)
}
输出:
title of this post is: a title title of this post is: another title