I need to loop through a array of arrays and calculate the sum of each array. The Json is a kendo-ui chart series with arrays of x,y coordinates. I need to return the sum of the x,y values. linq.js or javascript will work. thanks JSON
我需要遍历一个数组数组并计算每个数组的总和。 Json是一个带有x,y坐标数组的kendo-ui图表系列。我需要返回x,y值的总和。 linq.js或javascript将工作。谢谢JSON
var =series = [{
"style":"smooth",
"color":"blue",
"data":[
[600,30000],
[800,60000],
[1100,100000]
],
"name":"Subject Property",
"removeByNames":[
["Product1"],
["Product2"],
["Product3"]
],
"$$hashKey":"object:30"
}]
So for this example i would need to end up with this
因此,对于这个例子,我需要最终得到这个
var newSeries = [{
"style":"smooth",
"color":"blue",
"data":[
[30600],
[60800],
[101100]
],
"name":"Subject Property",
"removeByNames":[
["Product1"],
["Product2"],
["Product3"]
],
"$$hashKey":"object:30"
}]
4 个解决方案
#1
3
for(var i=0;i<series[0].data.length;i++){
var val = series[0].data[i];
newSeries.data[i] = val[0] + val[1];
}
#2
1
You can use loop and use reduce
您可以使用循环并使用reduce
var series = [{
...
}]
for (var i = 0; i < series.length; i++) {
for (var j = 0; j < series[i].data.length; j++) {
series[i].data[j] = series[i].data[j].reduce(function(p,c) {
return p + c;
});
}
}
Demo: http://jsfiddle.net/kv854c61/1/
演示:http://jsfiddle.net/kv854c61/1/
#3
1
you just need to loop the values in your data properties something like this..
你只需要在数据属性中循环这样的值。
for( var i - 0; i < series.length-1; i++){
for( var j - 0; j < series[i].data.length-1; i++){
var result = series[i].data[j][0] + series[i].data[j][1];
series[i].data[j] = result;
}
}
now it would make sense to add the new data array, so as not to overwrite
现在添加新数据阵列是有意义的,以免被覆盖
series[i].new_data[j] = result;
#4
1
Array.map
is quite useful in this case:
在这种情况下,Array.map非常有用:
// extracts the data entries of each item in the series
var data = series.map(function(item){ return item["data"]; });
function sum(point) {
return point[0] + point[1];
// in case point is of arbitrary dimension use Array.reduce:
// return point.reduce(function(prev, cur){ return prev + cur; }, 0);
}
var sums = data.map(function(arr){
return arr.map(function(point){
return sum(point);
});
});
// sums now contains an array of array of sums
// e.g. [[30600,60800,101100]]
#1
3
for(var i=0;i<series[0].data.length;i++){
var val = series[0].data[i];
newSeries.data[i] = val[0] + val[1];
}
#2
1
You can use loop and use reduce
您可以使用循环并使用reduce
var series = [{
...
}]
for (var i = 0; i < series.length; i++) {
for (var j = 0; j < series[i].data.length; j++) {
series[i].data[j] = series[i].data[j].reduce(function(p,c) {
return p + c;
});
}
}
Demo: http://jsfiddle.net/kv854c61/1/
演示:http://jsfiddle.net/kv854c61/1/
#3
1
you just need to loop the values in your data properties something like this..
你只需要在数据属性中循环这样的值。
for( var i - 0; i < series.length-1; i++){
for( var j - 0; j < series[i].data.length-1; i++){
var result = series[i].data[j][0] + series[i].data[j][1];
series[i].data[j] = result;
}
}
now it would make sense to add the new data array, so as not to overwrite
现在添加新数据阵列是有意义的,以免被覆盖
series[i].new_data[j] = result;
#4
1
Array.map
is quite useful in this case:
在这种情况下,Array.map非常有用:
// extracts the data entries of each item in the series
var data = series.map(function(item){ return item["data"]; });
function sum(point) {
return point[0] + point[1];
// in case point is of arbitrary dimension use Array.reduce:
// return point.reduce(function(prev, cur){ return prev + cur; }, 0);
}
var sums = data.map(function(arr){
return arr.map(function(point){
return sum(point);
});
});
// sums now contains an array of array of sums
// e.g. [[30600,60800,101100]]