【问题标题】:Covert object to array of combinations/objects将对象转换为组合/对象数组
【发布时间】:2016-04-08 03:48:45
【问题描述】:

使用 lodash 或下划线。我正在尝试转换这个对象:

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

到这里:

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

最好/最短的方法是什么?

【问题讨论】:

  • 相关/接近重复:stackoverflow.com/questions/36441510/…。顺便说一句,给你的问题一个更好的标题怎么样?我认为您正在寻找的是“排列”。
  • @torazaburo——我认为是组合,因为对象没有排序。 ;-)

标签: javascript json underscore.js lodash


【解决方案1】:

不确定 lodash 或 undesrcore。但是使用简单的 jquery 我已经做到了。看看吧。

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

【讨论】:

  • 干杯,我有这样的东西。但我的想法是我可以在混音中添加新的变化。 aka: "days": ["mon", "wed", "fri"] 我不需要编辑代码,只需要我的 JSON 文件。
【解决方案2】:

我认为您想将 object key 设置为新的 variable name 并组合内部对象值。

<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>

【讨论】:

    【解决方案3】:

    找到解决方案: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);
        });
      }
    });
    

    查看工作:

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

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 2018-12-04
      • 2021-06-16
      • 1970-01-01
      • 2020-06-15
      • 2016-03-11
      • 2019-07-11
      相关资源
      最近更新 更多