我需要一个函数将列表数组转换为过滤后的对象,它可以做出react
const list = [{ id:1, name:"name1"}, { id:2, name:"name2"}]
filtered = {1:name1, 2:name2}
我需要一个函数将列表数组转换为过滤后的对象,它可以做出react
const list = [{ id:1, name:"name1"}, { id:2, name:"name2"}]
filtered = {1:name1, 2:name2}
你可以使用reduce
它,试试这个:
const list = [{ id:1, name:"name1"}, { id:2, name:"name2"}]
const filtered = list.reduce((acc, {id, name}) => (acc[id] = name, acc), {});
console.log(filtered)