简体   繁体   English

结合多维数组,javascript

[英]Combine multidimensional array, javascript

I'm trying to do something simple but it doesn't work. 我正在尝试做一些简单的事情,但它不起作用。

I have two arrays that have a structure like this; 我有两个阵列,有这样的结构;

total ->
[0]-> [Day: 2016-04-19, Total: 23] 
[1]-> [Day: 2016-04-18, Total: 20]
failed ->
[0]-> [Day: 2016-04-19, Failed: 2] 
[1]-> [Day: 2016-04-18, Failed: 0]

I'm trying to add the "Failed" key with it's value to the "total" array but it just won't bite so the output looks something like this; 我正在尝试将“失败”键添加到“总”数组的值,但它不会咬,所以输出看起来像这样;

arr ->
[0]-> [Day: 2016-04-19, Total: 23, Failed: 2]
[1]-> [Day: 2016-04-18, Total: 23, Failed: 0]
...

The "total" I have assigned to var sql1 and "failed" I have assigned to var sql2 and then I've tried various functions and for loops like: 我已经分配给var sql1和“失败”的“总数”我已经分配给var sql2然后我尝试了各种函数和for循环,如:

for (var i = 0; i < sql1.length; i++) {
    sql1[i][2] = sql2[i][1];
}

Yet sql1 remains the same. 然而sql1仍然是一样的。 What am I doing wrong? 我究竟做错了什么?

I've also tried 我也试过了

sql1[i].push(sql2[i][1]);

But that didn't work either. 但这也不起作用。

You can use the Array.prototype.concat() for concatenate multiple arrays in this way: 您可以使用Array.prototype.concat()以这种方式连接多个数组:

var alpha = ['a', 'b', 'c'],
numeric = [1, 2, 3];

var alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric); // Result: ['a', 'b', 'c', 1, 2, 3]

However, looking at your code it seems you're messing up arrays with Object. 但是,查看代码时,您似乎正在使用Object搞乱数组。 Your code should look more like this: 您的代码看起来应该更像这样:

var total = [{
  Day: "2016-04-19",
  Total: 23
},
{
  Day: "2016-04-18",
  Total: 20
}];

var failed = [{
  Day: "2016-04-19",
  Total: 2
}, 
{
  Day: "2016-04-18",
  Total: 0
}]; 

console.log(total.concat(failed));

Try this: 尝试这个:

 var total = [ {Day: '2016-04-19', Total: 23}, {Day: '2016-04-18', Total: 20} ], failed = [ {Day: '2016-04-19', Failed: 2}, {Day: '2016-04-18', Failed: 0} ]; // clone total array var arr = total.slice().map(function(obj) { var result = {}; result.Day = obj.Day; result.Total = obj.Total; return result; }); for (var i=0; i<failed.length; ++i) { for (var j=0; j<arr.length; ++j) { if (failed[i].Day == arr[j].Day) { arr[j].Failed = failed[i].Failed; } } } document.body.innerHTML = '<pre>' + JSON.stringify(arr, true, 4) + '</pre>'; 

if Days are always in the same position in array you can simplify the code using: 如果Days总是在数组中的相同位置,您可以使用以下方法简化代码:

for (var i=0; i<failed.length; ++i) {
  arr[i].Failed = failed[i].Failed;
}

You can use some loops an a hash table for the correct keys. 您可以使用一些循环和一个哈希表来获得正确的密钥。

 var data = { total: [{ Day: '2016-04-19', Total: 23 }, { Day: '2016-04-18', Total: 20 }], failed: [{ Day: '2016-04-19', Failed: 2 }, { Day: '2016-04-18', Failed: 0 }] }, grouped = []; Object.keys(data).forEach(function (k) { data[k].forEach(function (a) { var key = { total: 'Total', failed: 'Failed' }[k]; if (!this[a.Day]) { this[a.Day] = { Day: a.Day, Total: 0, Failed: 0 }; grouped.push(this[a.Day]); } this[a.Day][key] += a[key]; }, this); }, Object.create(null)); document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM