0

I have two arrays lets say

var termRateOptionsArr = [{
    "term": 48,
    "rate": 2.99,
    "residualPercent": 0
}, {
    "term": 60,
    "rate": 1.99,
    "residualPercent": 0
}, {
    "term": 72,
    "rate": 0.99,
    "residualPercent": 0
}];

var cashDownOptionsArr = [{
    "cashdown": "2000"
}, {
    "cashdown": "4000"
}, {
    "cashdown": "6000"
}];

I want to combine these two arrays into a matrix combination using the following code:

var cartesian = function() {

  var combos = _.reduce(arguments, function(a, b) {
    return _.flatten(_.map(a, function(x) {
      return _.map(b, function(y) {
        return x.concat([y]);
      });
    }), true);
  }, [ [] ]);

  return _.flatten(_.map(combos, function(item) {
    return  _.extend(item[0], item[1]);
  }));

};

console.table( cartesian(termRateOptionsArr,cashDownOptionsArr));

Matrix Combination

I having issues combining I am getting only the last item array value instead of each item in the array. I think it has to do with some closure issue but not sure... Any help is appreciated..

Click here to see the plnkr

KayakDave
  • 24,636
  • 3
  • 65
  • 68
Ducati007
  • 265
  • 1
  • 5
  • 14
  • Why do you need two dimensions? Isn't it duplicating the same information, in this case, cash down options? It's accounting for waste of memory space. In javascript, two dimensional array is nothing but array of arrays. – Yogesh Dec 09 '13 at 19:29
  • How are you combining them? Please post your code, not only the data. – Bergi Dec 09 '13 at 20:08
  • @Bergi please see the link above for plnkr... – Ducati007 Dec 09 '13 at 20:32
  • @Ducati007: All I see there is a FOUC and then a white page. [Do not only post links to code](http://meta.stackexchange.com/a/125999/183280)! – Bergi Dec 09 '13 at 20:47

2 Answers2

0

According to this post: How can I add a key/value pair to a JavaScript object?, I think you should be able to use a syntax like this:

termRateOptionsArr[i].cashdown = cashDownOptionsArr[i].cashdown;
Community
  • 1
  • 1
C1pher
  • 1,933
  • 6
  • 33
  • 52
0

One of the issues is that _.extend() overwrites the object in it's first parameter ("destination"). So:

 return _.flatten(_.map(combos, function(item) {
    return  _.extend(item[0], item[1]);
  }));

overwrites your source cashDownOptionsArr array with newly merged objects.

One fix is to clone the destination object like this:

  return _.flatten(_.map(combos, function(item) {
    var clone0 = _.clone(item[0]);
    return  _.extend(clone0, item[1]);
  }));

plunker with underscore solution

However, since it's easy and much more performant to do this without underscore, I'd combine the two matrices like this instead:

var cartesian = function(a,b) {

  var combos= [];
  var i=0;
  for (var x=0; x<a.length;x++)
     for (var y=0; y<b.length;y++)
     { 
       combos[i]={};
       // Copy properties of a[x]
       for (var indx in a[x]) {
          combos[i][indx] = a[x][indx];
       }
       // Copy properties of b[y]
       for (var indx in b[y]) {
          combos[i][indx] = b[y][indx];
       }
      i++;
     }
   return combos;
};

Updated plunker

KayakDave
  • 24,636
  • 3
  • 65
  • 68