1

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:

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

Yet sql1 remains the same. What am I doing wrong?

I've also tried

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

But that didn't work either.

3 Answers 3

1

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>');

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Worked perfectly with some minor rewriting, had to replace { Day: a.Day, Total: 0, Failed: 0 }; with { Day: a.Day, Total: '', Failed: '' }; to remove the 0 that would otherwise show up as a prefix. Thanks!
1

You can use the Array.prototype.concat() for concatenate multiple arrays in this way:

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. 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));

Comments

1

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:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.