[
{ id: 10, color: "red" },
{ id: 20, color: "blue" },
{ id: 30, color: "green" }
].reduce((acc, cur) => ({ ...acc, [cur.color]: cur.id }), {})
//output:
{red: 10, blue: 20, green: 30}
const obj = {};
for (const key of yourArray) {
obj[key] = whatever;
}
myArray.find(item => item.isAstronaut)
// create object from array
const createObjectFromArray = (arr) => {
let obj = {};
for (let value of arr) {
obj[value] = ++obj[value] || 1;
}
return obj;
};