Javascript 对象内部for循环的迭代

Javascript 对象内部for循环的迭代,javascript,angular,ecmascript-6,Javascript,Angular,Ecmascript 6,我正在尝试使用for循环迭代我的数组,它工作得很好,现在如果我想在一个对象中迭代它,我该怎么做呢 下面是我的代码 for(设j=0;j

我正在尝试使用for循环迭代我的数组,它工作得很好,现在如果我想在一个对象中迭代它,我该怎么做呢

下面是我的代码

for(设j=0;j
预期输出:

种子详细信息:[
{object1},
{object2},。。。。
]

您可以定义一个getter:

private get myObj() {
   for (let j = 0; j < this.sowing.seedSownDetails.length; j++) {
      const myObj = {
        sowSeedInventoryId: this.sowing.seedSownDetails[j]?.inventoryId,
        quantity: this.sowing.seedSownDetails[j]?.quantity
      };
    }

    return myObj;
}

声明
this.myObj=[]位于顶部或内部
构造函数
,并按如下方式修改代码

for (let j = 0; j < sowing.seedSownDetails.length; j++) {
      this.myObj.push({
        sowSeedInventoryId: sowing.seedSownDetails[j]?.inventoryId,
        quantity: sowing.seedSownDetails[j]?.quantity
      });
    }

    this.addComments = {
      sowingId: sowing.sowingId,
      cropId: sowing.cropId,
      seedDetails: this.myObj, // You will get array of objects here as expected

      seedSowingDate: sowing.sowingDate,
      comments: form.value.comments,
    };

for(设j=0;j
for (let j = 0; j < sowing.seedSownDetails.length; j++) {
      this.myObj.push({
        sowSeedInventoryId: sowing.seedSownDetails[j]?.inventoryId,
        quantity: sowing.seedSownDetails[j]?.quantity
      });
    }

    this.addComments = {
      sowingId: sowing.sowingId,
      cropId: sowing.cropId,
      seedDetails: this.myObj, // You will get array of objects here as expected

      seedSowingDate: sowing.sowingDate,
      comments: form.value.comments,
    };