我有这个JSON输出:
[{"id":"15","title":"music"},{"id":"103","title":"movie"},{"id":"214","title":"book"}]
如何将其转换为Javascript中的数组:
items = array(
15 => 'music',
103 => 'movie',
214 => 'book'
);
答案 0 :(得分:2)
您可以将Object.assign
用于破坏对象并构建新对象。
var array = [{ id: "15", title: "music" }, { id: "103", title: "movie" }, { id:"214", title: "book" }],
object = Object.assign(...array.map(({ id, title }) => ({ [id]: title })));
console.log(object);

答案 1 :(得分:1)
您可以使用reduce(...)
:
var arr = [{"id":"15","title":"music"},{"id":"103","title":"movie"},{"id":"214","title":"book"}];
var obj = arr.reduce((acc, o) => { acc[o.id] = o.title; return acc; }, {});
console.log(obj);
// prints: {15: "music", 103: "movie", 214: "book"}
答案 2 :(得分:0)
似乎你在寻找像这样的对象
var x = [{
"id": "15",
"title": "music"
}, {
"id": "103",
"title": "movie"
}, {
"id": "214",
"title": "book"
}];
let temp = {};
x.forEach(function(item) {
temp[item.id] = item.title
})
console.log(temp)