Here is the object I'm working with:
const sampleData = [ {
main: 7,
second: 2
otherData: 'some string'
} ]
I want to create a new object only containing specific properties, like this:
const newDataObject = {
7: {
second: 2
}
}
I attempted to solve this using .reduce()
, but I'm struggling to exclude certain properties.
const newDataObject = sampleData.reduce((object, current, index) => {
return {
..object, [index]: current
};
}, {});
The resulting output is:
{ '0':
{ main: 7,
second: 3
}
}
What mistake am I making?
Thank you.