0

Using lodash or underscore. I'm trying to convert this object:

{
  "variations": {
    "versions": ["sport", "generic"],
    "devices": ["mobile", "tablet"]
  }
}

to this:

var variations = [{
  "version": "sport",
  "device": "mobile"
}, {
  "version": "sport",
  "device": "tablet"
}, {
  "version": "generic",
  "device": "mobile"
}, {
  "version": "generic",
  "device": "tablet"
}];

What's the best/shortest method to do this?

rickysullivan
  • 441
  • 1
  • 5
  • 12
  • 1
    Related/near dup: http://stackoverflow.com/questions/36441510/create-a-generator-to-iterate-object-property-permutations. By the way, how about giving your question a better title? I think what you are looking for is "permutations". –  Apr 08 '16 at 03:53
  • @torazaburo—I think combinations, since objects aren't ordered. ;-) – RobG Apr 08 '16 at 03:57

3 Answers3

0

Not sure with lodash or undesrcore. But with simple jquery i have done this. take a look.

var object={
  "variations": {
    "versions": ["sport", "generic"],
    "devices": ["mobile", "tablet"]
  }
};
var variations=[];
$.each(object.variations.versions, function(i, j) { 
    $.each(object.variations.devices, function(k, l) { 
        variations.push({version:j,device:l});
    });
});
Shibu Murugan
  • 31
  • 1
  • 8
  • Cheers, I had something like this. But the idea is that I can add a new variation to the mix. aka: "days": ["mon", "wed", "fri"] and I shouldn't need to edit the code, just my JSON file. – rickysullivan Apr 08 '16 at 04:23
0

I think you wanna set object key to new variable name and do combinations of inside object values.

<script type="text/javascript">
//here I created two object keys for more clear
var json ={
  "variations": {
    "versions": ["sport", "generic"],
    "devices": ["mobile", "tablet"]
  },
  "another_variations": {
    "versions": ["sport", "generic"],
    "devices": ["mobile", "tablet"]
  }
};

for(var i in json){     
     window[i] = []; //here window[variable] will make global variable
     ver = Object.keys(json[i])[0];//Object.keys(json[i]) get object keys ~["versions","devices"]
     dev = Object.keys(json[i])[1];
    window[i].push(
    {    
    [ver]:json[i].versions[0],
    [dev]:json[i].devices[0]
    },
    {
    [ver]:json[i].versions[0],
    [dev]:json[i].devices[1]
    },
    {
    [ver]:json[i].versions[1],
    [dev]:json[i].devices[0]
    },
    {
    [ver]:json[i].versions[1],
    [dev]:json[i].devices[1]
    });    
}
console.log(variations);        //here can call object key as a variable name if you 
console.log(another_variations);//don't use `window[variable]` in above, this will print undefined error
</script>
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
0

Found a solution using: https://gist.github.com/wassname/a882ac3981c8e18d2556

_.mixin({
  cartesianProductOf: function(args) {
    if (arguments.length > 1) args = _.toArray(arguments);
    // strings to arrays of letters
    args = _.map(args, opt => typeof opt === 'string' ? _.toArray(opt) : opt)
    return _.reduce(args, function(a, b) {
      return _.flatten(_.map(a, function(x) {
        return _.map(b, function(y) {
          return _.concat(x, [y]);
        });
      }), true);
    }, [
      []
    ]);
  },
  cartesianProductObj: function(optObj) {
    var keys = _.keys(optObj);
    var opts = _.values(optObj);
    var combs = _.cartesianProductOf(opts);
    return _.map(combs, function(comb) {
      return _.zipObject(keys, comb);
    });
  }
});

See working:

https://jsfiddle.net/rickysullivan/5ryf9jsa/

rickysullivan
  • 441
  • 1
  • 5
  • 12