[英]Create object array according to changing number of properties
I want to handle the array according to number of properties for a category.我想根据类别的属性数量处理数组。
I have created an array(array_a) using available properties, and I group that array value according to properties(array_colors,array_sizes).我使用可用属性创建了一个数组(array_a),并根据属性(array_colors,array_sizes)对该数组值进行分组。 for example now i have two arrays 'array_colors' and 'array_sizes', so in my code I for loop them and created an object array(array_b).例如,现在我有两个数组“array_colors”和“array_sizes”,所以在我的代码中我循环它们并创建了一个对象数组(array_b)。 but my issue is I want to handle dynamic number of properties.但我的问题是我想处理动态数量的属性。
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"
}
}
]
my expected array as above.I want to handle the properties such as color,size dynamically.我预期的数组如上。我想动态处理颜色、大小等属性。
using _.mergeWith()
you can merge the properties to a single object with the property names as the keys, and an array of values for each key.使用_.mergeWith()
您可以将属性合并为一个对象,以属性名称作为键,以及每个键的值数组。 Then reduce the object to an array of objects using _.flatMap()
(or Array.flatMap()
:然后使用_.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.