[英]spread operator on array object does not emit properties
我如何定义一个数组 object 以及在使用扩展运算符时将发出的属性,如果这样的事情甚至是可能的(如果不是,为什么?)。
I'm using arrays in a redux app so when I'm updating a store with a return {...state,newElement}
i lose the properties of the arrays in my state.
例如:
const object1 = [1,2,3];
Object.defineProperty(object1, 'pizza', {
value: 30,
writable: true,
enumerable:true,
configurable:true
});
console.log(...object1);
// output: 1 2 3
// my expected output: 1 2 3 30
谢谢
混合对象数组的实际迭代器是数组的迭代器(即Array.prototype.values
):
const arr = []; console.log(arr.values === arr[Symbol.iterator])
如果你想要一个混合对象数组,并期望它是完全可迭代的,你必须自己编写迭代器以覆盖数组的迭代器:
const object1 = [1,2,3]; Object.defineProperty(object1, 'pizza', { value: 30, writable: true, enumerable:true, configurable:true }); object1[Symbol.iterator] = function* () { // Write whatever implementation you need for(const key in this) { yield this[key]; } } console.log(...object1); // output: 1 2 3 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.