簡體   English   中英

數組 object 上的擴展運算符不發出屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM