多个数组元素的总和,并按索引 (x,y) 将此总和放入 table
Sum of multiple array elements and put this sum in table by index (x,y)
我有以下数据(由ajax加载):
[
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [18, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [16, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [14, 15, 11, 12, 9, 2, 33, 12, 2, 4],
}
]
我有 a 5x5 table.
如何计算 "value" 的每个多个元素的总和并将其放在右索引的 table 中?
我现在的 js 代码:
function risk(data) {
let sum = 0;
for (i = 0; i < data.length; i++) {
let b = data[i].Value;
console.log(b); // (10) [18, 15, 11, 12, 9, 2, 33, 12, 2, 4]
// (10) [16, 15, 11, 12, 9, 2, 33, 12, 2, 4]
// (10) [14, 15, 11, 12, 9, 2, 33, 12, 2, 4]
}
}
最后的结果一定是这样的:
您可以使用 reduce
构建 "result" 矩阵,在迭代时将 "Value" 添加到矩阵中适当的 x
和 y
坐标通过原始数据中的所有元素。
const data = [
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [18, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [16, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [14, 15, 11, 12, 9, 2, 33, 12, 2, 4],
}
];
const res = new Array(5).fill(0).map(n => new Array(5).fill(0));
data.reduce((acc, curr) => {
curr.Value.forEach((v, i) => acc[curr.x[i] - 1][curr.y[i] - 1] += v);
return res;
}, res);
console.log(res);
你可以拿一个对象来收集这些值。为了将结果返回到 table,您可以迭代坐标并获得一个值。
var data = [{ x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [18, 15, 11, 12, 9, 2, 33, 12, 2, 4] }, { x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [16, 15, 11, 12, 9, 2, 33, 12, 2, 4] }, { x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [14, 15, 11, 12, 9, 2, 33, 12, 2, 4] }],
sum = data.reduce((r, {x, y, Value}) =>
(x.forEach((x, i) => {
r[x] = r[x] || {};
r[x][y[i]] = (r[x][y[i]] || 0) + Value[i];
}), r), {});
console.log(sum);
我有以下数据(由ajax加载):
[
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [18, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [16, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [14, 15, 11, 12, 9, 2, 33, 12, 2, 4],
}
]
我有 a 5x5 table.
如何计算 "value" 的每个多个元素的总和并将其放在右索引的 table 中?
我现在的 js 代码:
function risk(data) {
let sum = 0;
for (i = 0; i < data.length; i++) {
let b = data[i].Value;
console.log(b); // (10) [18, 15, 11, 12, 9, 2, 33, 12, 2, 4]
// (10) [16, 15, 11, 12, 9, 2, 33, 12, 2, 4]
// (10) [14, 15, 11, 12, 9, 2, 33, 12, 2, 4]
}
}
最后的结果一定是这样的:
您可以使用 reduce
构建 "result" 矩阵,在迭代时将 "Value" 添加到矩阵中适当的 x
和 y
坐标通过原始数据中的所有元素。
const data = [
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [18, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [16, 15, 11, 12, 9, 2, 33, 12, 2, 4],
},
{
"x": [1, 1, 2, 2, 2, 3, 3, 3, 4, 5],
"y": [3, 5, 4, 2, 5, 3, 4, 5, 1, 5],
"Value": [14, 15, 11, 12, 9, 2, 33, 12, 2, 4],
}
];
const res = new Array(5).fill(0).map(n => new Array(5).fill(0));
data.reduce((acc, curr) => {
curr.Value.forEach((v, i) => acc[curr.x[i] - 1][curr.y[i] - 1] += v);
return res;
}, res);
console.log(res);
你可以拿一个对象来收集这些值。为了将结果返回到 table,您可以迭代坐标并获得一个值。
var data = [{ x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [18, 15, 11, 12, 9, 2, 33, 12, 2, 4] }, { x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [16, 15, 11, 12, 9, 2, 33, 12, 2, 4] }, { x: [1, 1, 2, 2, 2, 3, 3, 3, 4, 5], y: [3, 5, 4, 2, 5, 3, 4, 5, 1, 5], Value: [14, 15, 11, 12, 9, 2, 33, 12, 2, 4] }],
sum = data.reduce((r, {x, y, Value}) =>
(x.forEach((x, i) => {
r[x] = r[x] || {};
r[x][y[i]] = (r[x][y[i]] || 0) + Value[i];
}), r), {});
console.log(sum);