合并对象以获得数组中的平均值
Merging objects to obtain the average value in arrays
我正在尝试获取 scores 数组的平均值,并保留第一个对象的其他值。我无法管理如何遍历对象以获得预期的输出。
const bigArr = [
{
bigDummyData: "string0",
examples: [
{ smallDD: "string00", scores: [1, 1, 5] },
{ smallDD: "string01", scores: [2, 2, 4] },
{ smallDD: "string02", scores: [2, 2, 6] },
],
},
{
bigDummyData: "string1",
examples: [
{ smallDD: "string10", scores: [3, 3, 3] },
{ smallDD: "string11", scores: [2, 2, 2] },
{ smallDD: "string12", scores: [4, 4, 4] },
],
},
]
其预期输出:
output = {
bigDummyData: "string0",
examples: [
{ smallDD: "string00", scores: [2, 2, 4] },
{ smallDD: "string01", scores: [2, 2, 3] },
{ smallDD: "string02", scores: [3, 3, 5] },
],
}
如您所见,bigDummyData 和每个 smallDD 都是第一个对象留下的。
这是问题的简化示例,数组bigArr和示例已上传动态的,所以它们通常要长得多。
也许这会对你有所帮助
const bigArr = [
{
bigDummyData: "string0",
examples: [
{ smallDD: "string00", scores: [1, 1, 5] },
{ smallDD: "string01", scores: [2, 2, 4] },
{ smallDD: "string02", scores: [2, 2, 6] },
],
},
{
bigDummyData: "string1",
examples: [
{ smallDD: "string10", scores: [3, 3, 3] },
{ smallDD: "string11", scores: [2, 2, 2] },
{ smallDD: "string12", scores: [4, 4, 4] },
],
}
]
let firstElement = bigArr[0];
let length = bigArr.length;
bigArr.splice(0,1)
bigArr.forEach(({examples}) => {
examples.forEach(({scores},i) => {
firstElement.examples[i].scores = firstElement.examples[i].scores.map( (x,j) => x+scores[j]);
})
});
firstElement.examples.forEach(({scores},i) => {
firstElement.examples[i].scores = firstElement.examples[i].scores.map( (x) => x/length);
});
console.log(firstElement);
When to use Map
在对现有数组进行一些操作的基础上创建新数组。Official Documentation
Splice
splice() 方法通过删除或替换现有元素来更改数组的内容and/or 添加新元素Official Documentation
如果对你有帮助,请采纳。
我正在尝试获取 scores 数组的平均值,并保留第一个对象的其他值。我无法管理如何遍历对象以获得预期的输出。
const bigArr = [
{
bigDummyData: "string0",
examples: [
{ smallDD: "string00", scores: [1, 1, 5] },
{ smallDD: "string01", scores: [2, 2, 4] },
{ smallDD: "string02", scores: [2, 2, 6] },
],
},
{
bigDummyData: "string1",
examples: [
{ smallDD: "string10", scores: [3, 3, 3] },
{ smallDD: "string11", scores: [2, 2, 2] },
{ smallDD: "string12", scores: [4, 4, 4] },
],
},
]
其预期输出:
output = {
bigDummyData: "string0",
examples: [
{ smallDD: "string00", scores: [2, 2, 4] },
{ smallDD: "string01", scores: [2, 2, 3] },
{ smallDD: "string02", scores: [3, 3, 5] },
],
}
如您所见,bigDummyData 和每个 smallDD 都是第一个对象留下的。
这是问题的简化示例,数组bigArr和示例已上传动态的,所以它们通常要长得多。
也许这会对你有所帮助
const bigArr = [
{
bigDummyData: "string0",
examples: [
{ smallDD: "string00", scores: [1, 1, 5] },
{ smallDD: "string01", scores: [2, 2, 4] },
{ smallDD: "string02", scores: [2, 2, 6] },
],
},
{
bigDummyData: "string1",
examples: [
{ smallDD: "string10", scores: [3, 3, 3] },
{ smallDD: "string11", scores: [2, 2, 2] },
{ smallDD: "string12", scores: [4, 4, 4] },
],
}
]
let firstElement = bigArr[0];
let length = bigArr.length;
bigArr.splice(0,1)
bigArr.forEach(({examples}) => {
examples.forEach(({scores},i) => {
firstElement.examples[i].scores = firstElement.examples[i].scores.map( (x,j) => x+scores[j]);
})
});
firstElement.examples.forEach(({scores},i) => {
firstElement.examples[i].scores = firstElement.examples[i].scores.map( (x) => x/length);
});
console.log(firstElement);
When to use Map
在对现有数组进行一些操作的基础上创建新数组。Official Documentation
Splice
splice() 方法通过删除或替换现有元素来更改数组的内容and/or 添加新元素Official Documentation
如果对你有帮助,请采纳。