Reputation: 23207
I've an array like this:
let aa = [
Object.defineProperty({alignedType: "t"}, "f1", {value: 1}),
Object.defineProperty({alignedType: "t"}, "f2", {value: 2}),
Object.defineProperty({alignedType: "t"}, "f3", {value: 3}),
Object.defineProperty({alignedType: "t"}, "f4", {value: 4})
];
I need to reduce this array an simple object like:
let yyy = aa.reduce((acc, e) => {
return {...acc, ...e};
});
I'm running against yyy
only contains a single object with only alignedType: "t"
. It has no more properties.
Here you have debugger:
The object I was expecting is: {alignedType: "t", f1: 1, f2: 2, f3: 3, f4: 4}
.
Any ideas?
Upvotes: 0
Views: 95
Reputation: 580
You need to set { enumerable: true }
to say that this property shows up during enumeration of the properties on the corresponding object:
let aa = [
Object.defineProperty({ alignedType: 't' }, 'f1', { value: 1, enumerable: true }),
Object.defineProperty({ alignedType: 't' }, 'f2', { value: 1, enumerable: true }),
Object.defineProperty({ alignedType: 't' }, 'f3', { value: 1, enumerable: true }),
Object.defineProperty({ alignedType: 't' }, 'f4', { value: 1, enumerable: true }),
];
let yyy = aa.reduce((acc, e) => {
return {...acc, ...e};
});
console.log(yyy);
See enumerable on developer.mozilla.org
Update
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax:
The Rest/Spread Properties for ECMAScript proposal (stage 4) adds spread properties to object literals. It copies own enumerable properties from a provided object onto a new object.
Upvotes: 1