繁体   English   中英

根据更改的属性数量创建对象数组

[英]Create object array according to changing number of properties

我想根据类别的属性数量处理数组。

我使用可用属性创建了一个数组(array_a),并根据属性(array_colors,array_sizes)对该数组值进行分组。 例如,现在我有两个数组“array_colors”和“array_sizes”,所以在我的代码中我循环它们并创建了一个对象数组(array_b)。 但我的问题是我想处理动态数量的属性。

var array_a = [
   {
  "color":{
     "id":2,
     "name":"Green"
   }
   },
   {
  "color":{
     "id":3,
     "name":"Yellow"
   }
   },
   {
  "size":{
     "id":5,
     "name":"16"
   }
   },
   {
  "size":{
     "id":6,
     "name":"17"
   }
   },
   {
  "size":{
     "id":7,
     "name":"18"
  }
  }
];
array_colors = array_a.filter(function(myObject) {
    return myObject.hasOwnProperty('color');
  });

array_sizes = array_a.filter(function(myObject) {
 return myObject.hasOwnProperty('size');
 });

array_b = [];

for(var i = 0; i < array_colors.length; i++) {
for (var j = 0; j < array_sizes.length; j++) {
array_b.push( {
  "is_available":true,
  "qty":0,
  "price":0,
  "color":array_colors[i],
  "size":array_sizes[j]
});
}
}


array_b = [  
  {  
  "is_available":true,
  "qty":0,
  "price":0,
  "color":{  
     "id":2,
     "name":"Green"
      },
  "size":{  
     "id":5,
     "name":"16",
     "text":"16"
      }
   },
   {  
  "is_available":true,
  "qty":0,
  "price":0,
  "color":{  
     "id":2,
     "name":"Green"
      },
  "size":{  
     "id":6,
     "name":"17",
     "text":"17"
      }
   },
   {  
  "is_available":true,
  "qty":0,
  "price":0,
  "color":{  
     "id":2,
     "name":"Green"
      },
  "size":{  
     "id":7,
     "name":"18",
     "text":"18"
      }
   },
   {  
  "is_available":true,
  "qty":0,
  "price":0,
  "color":{  
     "id":3,
     "name":"Yellow"
      },
  "size":{  
     "id":5,
     "name":"16",
     "text":"16"
      }
   },
   {  
  "is_available":true,
  "qty":0,
  "price":0,
  "color":{  
     "id":3,
     "name":"Yellow"
      },
  "size":{  
     "id":6,
     "name":"17",
     "text":"17"
       }
   },
   {  
  "is_available":true,
  "qty":0,
  "price":0,
  "color":{  
     "id":3,
     "name":"Yellow"
      },
  "size":{  
     "id":7,
     "name":"18",
     "text":"18"
      }
   }
]

我预期的数组如上。我想动态处理颜色、大小等属性。

使用_.mergeWith()您可以将属性合并为一个对象,以属性名称作为键,以及每个键的值数组。 然后使用_.flatMap() (或Array.flatMap()将对象减少为对象数组:

 const array_a = [{"color":{"id":2,"name":"Green"}},{"color":{"id":3,"name":"Yellow"}},{"size":{"id":5,"name":"16"}},{"size":{"id":6,"name":"17"}},{"size":{"id":7,"name":"18"}}]; const getAllCombinations = _.flow([ arr => _.mergeWith({}, ...arr, (o, s) => _.isArray(o) ? [...o, s.name] : [s.name]), props => _.reduce(props, (r, values, k) => _.isEmpty(r) ? values.map(v => ({ is_available: true, qty: 0, price: 0, [k]: v, })) : _.flatMap(r, o => values.map(v => ({ ...o, [k]: v }))), []) ]); console.log(getAllCombinations(array_a));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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