我在对象中有一个数组,如下所示:
[
{ "id": 244, "title": "myBook.pdf", "author": "Kevin"},
{ "id": 370, "title": "Bookpress.pdf", "author": "Justin"},
{ "id": 433, "title": "Uptown.pdf", "author": "David"},
]
如何获取所有标题数据的所有数据?所需数据是: myBook.pdf,Bookpress.pdf,Uptown.pdf
答案 0 :(得分:0)
如果您使用的是lodash,则只需执行此操作
即可var data = [
{ "id": 244, "title": "myBook.pdf", "author": "Kevin"},
{ "id": 370, "title": "Bookpress.pdf", "author": "Justin"},
{ "id": 433, "title": "Uptown.pdf", "author": "David"},
]
var result = _(data).map("title").value();
给你
result
(3) ["myBook.pdf", "Bookpress.pdf", "Uptown.pdf"]
但是,因为这听起来像是一个任务问题,你应该尝试自己做这个没有库。
答案 1 :(得分:0)
只需使用Array#map
函数.use join(',')
函数来应用逗号
var arr = [
{ "id": 244, "title": "myBook.pdf", "author": "Kevin"},
{ "id": 370, "title": "Bookpress.pdf", "author": "Justin"},
{ "id": 433, "title": "Uptown.pdf", "author": "David"},
]
console.log(arr.map(a=> a.title))
console.log(arr.map(a=> a.title).join(','))

答案 2 :(得分:0)
使用for循环遍历数组并使用javascript对象表示法来获取所需的对象属性并将其附加到html中或以您喜欢的任何方式使用。
request.getErrorStream()
var arr =[
{ "id": 244, "title": "myBook.pdf", "author": "Kevin"},
{ "id": 370, "title": "Bookpress.pdf", "author": "Justin"},
{ "id": 433, "title": "Uptown.pdf", "author": "David"},
]
for (var i =0;i < arr.length;i++){
var el = document.getElementById("demo");
el.innerHTML += arr[i].title
}