简体   繁体   中英

combine multidimensional array into key based dictionary object

I am trying to create a price schema for a product, basing on its color, size and material. My current way of implementation is to maintain 1 JSON object which contains all the options and trying to generate another JSON object using all possible combination of its options. For example

var productOption = {
  "color":["red","green","yellow"],
  "size":["S","M","L"],
  "material":["leather","linen"]
}

what I want to create something like follow

[
  {
  "red":{
    "S":{
      "leather":{
        "cost":100,"available":true
        }
      }
    }
  },
  {
  "green":{
    "S":{
      "leather":{
        "cost":100,"available":true
        }
      }
    }
  },
  ....

]

so I can do something like price["red"]["S"]["leather"] to access the price for red color, leather, in small size. Is there a easy way to do it? either in Javascript or Python.

EDIT:

What if some product only has color and size or color only?

var productOption= { color :[...],size:[...]};

EDIT 2:

I made up following code to solve my issue. If you have any suggestion on how I can improve my code. Please let me know.

// I am using lodash for those underscore signs.
var processPrice = function(){
  price = {};
  var keys = _.keys(_.pick(priceOption,function(value,key){
    return !_.isEmpty(value);
  }));
  if(keys.length>0){
    genMatrix(keys,[],price);  
  }
};
var genMatrix = function(keys,options,obj){
  var key = keys[0];
  if(keys.length>1){
    var subKeys = _.compact(keys);
      subKeys.shift();
    _.each(priceOption[key],function(opt){
      var subOption = _.compact(options);
      subOption.push(opt.value);
      obj[opt.value]={};
      genMatrix(subKeys,subOption,obj[opt.value]); 
    });
  }else{
    _.each(priceOption[key],function(opt){
      obj[opt.value]={cost: 100, available: true};
    });
  }
};

Python version

from itertools import product
result = []
for col, siz, mat in product(*(data["color"], data["size"], data["material"])):
    result.append({col: {siz: {mat: {'available': True, 'cost': 100}}}})
print result

Output

[{'red': {'S': {'leather': {'available': True, 'cost': 100}}}},
 {'red': {'S': {'linen': {'available': True, 'cost': 100}}}},
 {'red': {'M': {'leather': {'available': True, 'cost': 100}}}},
 {'red': {'M': {'linen': {'available': True, 'cost': 100}}}},
 {'red': {'L': {'leather': {'available': True, 'cost': 100}}}},
 {'red': {'L': {'linen': {'available': True, 'cost': 100}}}},
 {'green': {'S': {'leather': {'available': True, 'cost': 100}}}},
 {'green': {'S': {'linen': {'available': True, 'cost': 100}}}},
 {'green': {'M': {'leather': {'available': True, 'cost': 100}}}},
 {'green': {'M': {'linen': {'available': True, 'cost': 100}}}},
 {'green': {'L': {'leather': {'available': True, 'cost': 100}}}},
 {'green': {'L': {'linen': {'available': True, 'cost': 100}}}},
 {'yellow': {'S': {'leather': {'available': True, 'cost': 100}}}},
 {'yellow': {'S': {'linen': {'available': True, 'cost': 100}}}},
 {'yellow': {'M': {'leather': {'available': True, 'cost': 100}}}},
 {'yellow': {'M': {'linen': {'available': True, 'cost': 100}}}},
 {'yellow': {'L': {'leather': {'available': True, 'cost': 100}}}},
 {'yellow': {'L': {'linen': {'available': True, 'cost': 100}}}}]

JavaScript version:

var result = [];

for (var i = 0; i < data["color"].length; i += 1) {
    var color = data["color"][i];
    for (var j = 0; j < data["size"].length; j += 1) {
        var size = data["size"][j];
        for (var k = 0; k < data["material"].length; k += 1) {
            var material = data["material"][k], obj = {};
            obj[color] = {};
            obj[color][size] = {};
            obj[color][size][material] = {cost: 100, available: true};
            result.push(obj);
        }
    }
}
console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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