I need a function to convert the list array into the filtered object, which works to react
const list = [{ id:1, name:"name1"}, { id:2, name:"name2"}] filtered = {1:name1, 2:name2}
You can use reduce for that, try this:
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)
1.4m articles
1.4m replys
5 comments
56.9k users