Can any please help me for converting following arrays into object:
let data = [
{ name: "A", job: "soft", address: "ggn" },
{ name: "B", job: "software", address: "GGN" },
{ name: "C", job: "software", address: "GGN" },
];
Output: {
“A”:{job: “software”, address: “GGN”},
“B”:{job: “software”, address: “GGN”},
“C”:{job: “software”, address: “GGN”}
};
Here I don't want name and its value of objects.
I tried using the following solution:
const output = data.reduce((object, x) => {
return { ...object, [x.name]: x };
}, {});
But I am getting different answer. Can somebody help by giving optimum solution. We can solve it using reduce and rest operator.