我有一个对象:
"items":[{"id": 1, "name": "A name", "content": "A content"},
{"id": 2, "name": "A name", "content": "A content"},
{"id": 3, "name": "A name", "content": "A content"}]
我想将此对象转换为数组。
我尝试了
const idItem = Object.keys(obj['items']).map(key => ({id: key,
value: obj['items']['id']}));
结果:
[{"id":"1"},{"id":"2"},{"id":"3"}]
它可以工作,但是我不知道如何转换所有内容(名称和内容)。
答案 0 :(得分:0)
使用传播算子
const items = [{"id": 1, "name": "A name", "content": "A content"},
{"id": 2, "name": "A name", "content": "A content"},
{"id": 3, "name": "A name", "content": "A content"}];
console.log({...items}); // --> {0: {...}, 1: {...}, 2: {...}}
答案 1 :(得分:0)
您是否正在尝试做类似的事情
var obj={
"items":[{"id": 1, "name": "A name", "content": "A content"},
{"id": 2, "name": "A name", "content": "A content"},
{"id": 3, "name": "A name", "content": "A content"}
]
}
var data=obj['items'];
console.log(data)
var item=data.find(i=> i.id==3);
console.log(item);