对象数组中字段的平均值

时间:2019-10-02 10:35:55

标签: javascript

我有以下对象数组:

[
    {
        items: {
            "1554038371930_ajhnms9ft" : {
                "mark" : 5
            },
            "1554038905938_le34li2cg" : {
                "mark" : 2
            },
            "1554039157958_kwab8rj5f" : {
                "mark" : 4
            },
            "1554039174126_p47ugwkbv" : {
                "mark" : 1
            }
        }
    },
    {
        items: {
            "1554038371930_ajhnms9ft" : {
                "mark" : 1
            },
            "1554038905938_le34li2cg" : {
                "mark" : 5
            },
            "1554039157958_kwab8rj5f" : {
                "mark" : 5
            },
            "1554039174126_p47ugwkbv" : {
                "mark" : 2
            }
        }
    },
    {
        items: {
            "1554038371930_ajhnms9ft" : {
                "mark" : 4
            },
            "1554038905938_le34li2cg" : {
                "mark" : 3
            },
            "1554039157958_kwab8rj5f" : {
                "mark" : 3
            },
            "1554039174126_p47ugwkbv" : {
                "mark" : 4
            }
        }
    },
    {
        items: {
            "1554038371930_ajhnms9ft" : {
                "mark" : 1
            },
            "1554038905938_le34li2cg" : {
                "mark" : 2
            },
            "1554039157958_kwab8rj5f" : {
                "mark" : 5
            },
            "1554039174126_p47ugwkbv" : {
                "mark" : 4
            }
        }
    }
]

并且我想获取chack项目的平均值,因此结果如下:

{
    items: {
        "1554038371930_ajhnms9ft" : {
            "mark" : 2.75
        },
        "1554038905938_le34li2cg" : {
            "mark" : 3
        },
        "1554039157958_kwab8rj5f" : {
            "mark" : 4.25
        },
        "1554039174126_p47ugwkbv" : {
            "mark" : 2.75
        }
    }
}

实现这一目标的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

这将为您工作,对数组和对象键使用一些循环:

var itemsArr = [{
    items: {
      "1554038371930_ajhnms9ft": {
        "mark": 5
      },
      "1554038905938_le34li2cg": {
        "mark": 2
      },
      "1554039157958_kwab8rj5f": {
        "mark": 4
      },
      "1554039174126_p47ugwkbv": {
        "mark": 1
      }
    }
  },
  {
    items: {
      "1554038371930_ajhnms9ft": {
        "mark": 1
      },
      "1554038905938_le34li2cg": {
        "mark": 5
      },
      "1554039157958_kwab8rj5f": {
        "mark": 5
      },
      "1554039174126_p47ugwkbv": {
        "mark": 2
      }
    }
  },
  {
    items: {
      "1554038371930_ajhnms9ft": {
        "mark": 4
      },
      "1554038905938_le34li2cg": {
        "mark": 3
      },
      "1554039157958_kwab8rj5f": {
        "mark": 3
      },
      "1554039174126_p47ugwkbv": {
        "mark": 4
      }
    }
  },
  {
    items: {
      "1554038371930_ajhnms9ft": {
        "mark": 1
      },
      "1554038905938_le34li2cg": {
        "mark": 2
      },
      "1554039157958_kwab8rj5f": {
        "mark": 5
      },
      "1554039174126_p47ugwkbv": {
        "mark": 4
      }
    }
  }
];
var tempObj = {};
itemsArr.forEach((itemsObj) => {
  let items = itemsObj.items;
  Object.keys(items).forEach((key) => {
    if(tempObj[key]) {
      tempObj[key].mark += items[key].mark;
      tempObj[key].count++;
    } else {
      tempObj[key] = {
        mark: items[key].mark,
        count: 1
      };
    }
  });
});
Object.keys(tempObj).forEach((key) => {
  tempObj[key].mark = tempObj[key].mark/tempObj[key].count;
  delete tempObj[key].count;
});
console.log(tempObj);

答案 1 :(得分:-1)

创建一个包含最终结果的对象,遍历所有项目并获取平均值。 示例:

const items = {};
for (let i = 0; i < arr.length; i++) {
  const keys = Object.keys(arr[i].items)
  for (let j = 0; j < keys.length; j++) {
    items[keys[j]] = items[keys[j]] === undefined ? 
      arr[i].items[keys[j]].mark : 
      (items[keys[j]] + arr[i].items[keys[j]].mark) / 2;
  }
}