【问题标题】:Assign datas to an object in a for将数据分配给 for 中的对象
【发布时间】:2019-01-23 00:12:42
【问题描述】:

我正在尝试将循环数据添加到对象。

subItem.allAvgObj = {};

for(var i = 0; i < subItem.implementations.length; i++) {

if (subItem.implementations[i].avg_score) {

        Object.assign(subItem.allAvgObj, {
          scoreAvg: subItem.implementations[i].avg_score,
          scorePonderation: subItem.implementations[i].ponderation,
        })

    }
}

只有,我的循环中只有最后一个被分配的对象。

{
 "scoreAvg": 8,
 "scorePonderation": 10
}

我尝试了一个数组,它可以工作(但它是一个具有单个值的数组,但循环可以工作)

subItem.allAvgArray.push(subItem.implementations[i].avg_score);

返回:

[
  13.5,
  16,
  8
]

如何制作这样的对象?

{
  "scoreAvg": 13.5,
  "scorePonderation": 20
},
{
  "scoreAvg": 16,
  "scorePonderation": 20
},
{
  "scoreAvg": 8,
  "scorePonderation": 10
}

谢谢

【问题讨论】:

标签: javascript


【解决方案1】:

创建对象数组的更简单方法可能是使用.filter() 删除没有avg_score() 的对象,然后使用.map() 从每个项目创建结果对象。

let result = subItem.implementations
  .filter(i => i.avg_score)
  .map(i => ({
    scoreAvg: i.avg_score,
    scorePonderation: i.ponderation
  }));

const subItem = {
  implementations: [{
      avg_score: 15.7,
      ponderation: 20
    },
    {
      avg_score: 0,
      ponderation: 15
    }, {
      avg_score: 6.8,
      ponderation: 10
    }
  ]
}

let result = subItem.implementations
  .filter(i => i.avg_score)  //remove items without avg_score
  .map(i => ({               //turn the remaining items into objects
    scoreAvg: i.avg_score,
    scorePonderation: i.ponderation
  }));
  
console.log(result);

或者,reduce() 一次性解决方案。

let result = subItem.implementations
  .reduce((acc,i) => 
    i.avg_score 
      ? [...acc, {scoreAvg: i.avg_score, scorePonderation: i.ponderation}] 
      : acc
  ,[]);

const subItem = {
  implementations: [{
      avg_score: 15.7,
      ponderation: 20
    },
    {
      avg_score: 0,
      ponderation: 15
    }, {
      avg_score: 6.8,
      ponderation: 10
    }
  ]
}

let result = subItem.implementations
  .reduce((acc,i) => 
    i.avg_score  //if avg_score, add an object. else, do nothing.
      ? [...acc, {scoreAvg: i.avg_score, scorePonderation: i.ponderation}] 
      : acc
  ,[]);

console.log(result);

请注意,reduce() 解决方案在性能上弥补了它在可读性方面的不足。

【讨论】:

  • 从性能的角度来看,reduce 解决方案比 map + filter 更好的解决方案。
  • 你是对的,虽然它真的取决于数组的长度。如果它不是一个大数组,则性能差异可以忽略不计。在这种情况下,为了可读性,有些人更喜欢filter + map。总而言之,尽管我同意你的观点,并修改了我的回答以说明这一点。
【解决方案2】:

Object.assign 用于浅层合并对象,而不是创建看起来是您想要的对象数组。

试试:

subItem.allAvgObj = [];

for(var i = 0; i < subItem.implementations.length; i++) {

if (subItem.implementations[i].avg_score) {

        subItem.allAvgObj.push({
          scoreAvg: subItem.implementations[i].avg_score,
          scorePonderation: subItem.implementations[i].ponderation,
        })

    }
}

预期的输出应该是:

[
    {
      "scoreAvg": 13.5,
      "scorePonderation": 20
    },
    {
      "scoreAvg": 16,
      "scorePonderation": 20
    },
    {
      "scoreAvg": 8,
      "scorePonderation": 10
    }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-15
    • 2012-04-26
    • 2010-11-22
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    相关资源
    最近更新 更多