I am facing an issue with transforming an array in JavaScript into a new object. Here is the initial array:
data = [
{
item: "Banana",
path: "fruit"
},
{
message: "Volvo",
path: "car"
},
{
message: "Cat",
path: "animal"
}
]
The desired output is to create a new object like this:
data = {
fruit: "Banana",
car: "Volvo",
animal: "Cat"
}
I attempted to achieve this using a loop, but ran into some issues. Here is the code snippet:
var newData = [];
data.map(value => {
newData[value.path] = value.message
});
However, the result I'm getting is structured differently. The loop returns an array instead of the desired object:
data = [
{
fruit: "Banana"
},
{
car: "Volvo"
},
{
animal: "Cat"
}
]
Could you please provide guidance on how to solve this issue? Thank you.