我有一个关联数组,如下所示。当子类别超过5时,我需要将它分成2个或更多部分。是否可能? 我需要javascript代码来分割这个数组动态,这样一个类别应该只有5个子类别,如果一个类别中有超过5个子类别,它应该创建另一个具有相同名称的类别与剩余子类别
var obj = [
{
"categoryId": "57f22e84c3ed2bd632c061bf",
"categoryName": "VODKA"
},
"subCategory": [
{
"subCategoryId": "57fdf5a3c3ed2bd632c06225",
"subCategoryName": "Grey goose"
},
{
"subCategoryId": "57fdf5fcc3ed2bd632c06227",
"subCategoryName": "Absolut Elyx"
},
{
"subCategoryId": "57fdf5c2c3ed2bd632c06226",
"subCategoryName": "Belvedere"
},
{
"subCategoryId": "57fdf627c3ed2bd632c06229",
"subCategoryName": "Absolut"
},
{
"subCategoryId": "57fdf61fc3ed2bd632c06228",
"subCategoryName": "Finlandia"
},
{
"subCategoryId": "57fdf64cc3ed2bd632c0622a",
"subCategoryName": "Absolut flavoured"
}
]
},
{
"categoryId": "57f22f18c3ed2bd632c061c4",
"categoryName": "SCOTCH WHISKY"
},
"priority": 2,
"subCategory": [
{
"subCategoryId": "57fdf163c3ed2bd632c06217",
"subCategoryName": "Blue lable"
},
{
"subCategoryId": "57fdf236c3ed2bd632c06218",
"subCategoryName": "Royal sulte"
},
{
"subCategoryId": "57fdf24ac3ed2bd632c06219",
"subCategoryName": "Chivas 18yr"
},
{
"subCategoryId": "57fdf2bfc3ed2bd632c0621a",
"subCategoryName": "J/W Gold lable"
},
{
"subCategoryId": "57fdf2f9c3ed2bd632c0621d",
"subCategoryName": "Chivas extra"
},
{
"subCategoryId": "57fdf2d7c3ed2bd632c0621b",
"subCategoryName": "Chivas 12yr"
},
{
"subCategoryId": "57fdf2edc3ed2bd632c0621c",
"subCategoryName": "J/W Black lable"
},
{
"subCategoryId": "57fdf32ac3ed2bd632c0621e",
"subCategoryName": "J/W Red lable"
}
]
}]
预期结果应为
var expectedResult = [
{
"categoryId": "57f22e84c3ed2bd632c061bf",
"categoryName": "VODKA"
},
"subCategory": [
{
"subCategoryId": "57fdf5a3c3ed2bd632c06225",
"subCategoryName": "Grey goose"
},
{
"subCategoryId": "57fdf5fcc3ed2bd632c06227",
"subCategoryName": "Absolut Elyx"
},
{
"subCategoryId": "57fdf5c2c3ed2bd632c06226",
"subCategoryName": "Belvedere"
},
{
"subCategoryId": "57fdf627c3ed2bd632c06229",
"subCategoryName": "Absolut"
},
{
"subCategoryId": "57fdf61fc3ed2bd632c06228",
"subCategoryName": "Finlandia"
},
]
},
{
"categoryId": "57f22e84c3ed2bd632c061bf",
"categoryName": "VODKA"
},
"subCategory": [
{
"subCategoryId": "57fdf64cc3ed2bd632c0622a",
"subCategoryName": "Absolut flavoured"
}
]
},
{
"categoryId": "57f22f18c3ed2bd632c061c4",
"categoryName": "SCOTCH WHISKY"
},
"priority": 2,
"subCategory": [
{
"subCategoryId": "57fdf163c3ed2bd632c06217",
"subCategoryName": "Blue lable"
},
{
"subCategoryId": "57fdf236c3ed2bd632c06218",
"subCategoryName": "Royal sulte"
},
{
"subCategoryId": "57fdf24ac3ed2bd632c06219",
"subCategoryName": "Chivas 18yr"
},
{
"subCategoryId": "57fdf2bfc3ed2bd632c0621a",
"subCategoryName": "J/W Gold lable"
},
{
"subCategoryId": "57fdf2f9c3ed2bd632c0621d",
"subCategoryName": "Chivas extra"
}
]
},
{
"categoryId": "57f22f18c3ed2bd632c061c4",
"categoryName": "SCOTCH WHISKY"
},
"priority": 2,
"subCategory": [
{
"subCategoryId": "57fdf2d7c3ed2bd632c0621b",
"subCategoryName": "Chivas 12yr"
},
{
"subCategoryId": "57fdf2edc3ed2bd632c0621c",
"subCategoryName": "J/W Black lable"
},
{
"subCategoryId": "57fdf32ac3ed2bd632c0621e",
"subCategoryName": "J/W Red lable"
}
]
}
]
答案 0 :(得分:2)
考虑单个category
条目超过5个subcategory
- 如果需要,您可以使用Array.prototype.reduce
和Object.assign
将其拆分为两个或更多元素:
var array=[{"categoryId":"57f22f18c3ed2bd632c061c4","categoryName":"SCOTCH WHISKY","categoryimageURL":{"thumbnail":"link","original":"link"},"priority":2,"subCategory":[{"subCategoryId":"57fdf163c3ed2bd632c06217","subCategoryName":"Blue lable","basePrice":599,"currentPrice":599,"highPrice":599,"lowPrice":599,"currentStock":25,"toggle":0},{"subCategoryId":"57fdf236c3ed2bd632c06218","subCategoryName":"Royal sulte","basePrice":599,"currentPrice":599,"highPrice":599,"lowPrice":599,"currentStock":25,"toggle":0},{"subCategoryId":"57fdf24ac3ed2bd632c06219","subCategoryName":"Chivas 18yr","basePrice":349,"currentPrice":349,"highPrice":349,"lowPrice":349,"currentStock":50,"toggle":0},{"subCategoryId":"57fdf2bfc3ed2bd632c0621a","subCategoryName":"J/W Gold lable","basePrice":299,"currentPrice":299,"highPrice":299,"lowPrice":299,"currentStock":50,"toggle":0},{"subCategoryId":"57fdf2f9c3ed2bd632c0621d","subCategoryName":"Chivas extra","basePrice":299,"currentPrice":299,"highPrice":299,"lowPrice":299,"currentStock":50,"toggle":0},{"subCategoryId":"57fdf2d7c3ed2bd632c0621b","subCategoryName":"Chivas 12yr","basePrice":199,"currentPrice":199,"highPrice":199,"lowPrice":199,"currentStock":125,"toggle":0},{"subCategoryId":"57fdf2edc3ed2bd632c0621c","subCategoryName":"J/W Black lable","basePrice":199,"currentPrice":199,"highPrice":199,"lowPrice":199,"currentStock":125,"toggle":0},{"subCategoryId":"57fdf32ac3ed2bd632c0621e","subCategoryName":"J/W Red lable","basePrice":129,"currentPrice":129,"highPrice":129,"lowPrice":129,"currentStock":125,"toggle":0}]}];
var arrays = [],
size = 5;
var result = array.reduce(function(prev, curr) {
// split the subcategories in groups of 5
arrays = [];
while (curr.subCategory.length > 0)
arrays.push(curr.subCategory.splice(0, size));
// create a new element for each group
arrays.forEach(function(element) {
curr.subCategory = element;
prev.push(Object.assign({}, curr));
});
return prev;
}, []);
console.log(result);
.as-console-wrapper {top: 0;max-height: 100%!important;}
答案 1 :(得分:0)
喜欢这个?他们都有超过5个元素,所以我不确定这是你想要的。
var obj = [
{
"categoryId": "57f22e84c3ed2bd632c061bf",
"categoryName": "VODKA",
"priority": 1,
"categoryimageURL": {
"thumbnail": "link",
"original": "link"
},
"subCategory": [
{
"subCategoryId": "57fdf5a3c3ed2bd632c06225",
"subCategoryName": "Grey goose",
"basePrice": 179,
"currentPrice": 179,
"highPrice": 179,
"lowPrice": 179,
"currentStock": 50,
"toggle": 0
},
{
"subCategoryId": "57fdf5fcc3ed2bd632c06227",
"subCategoryName": "Absolut Elyx",
"basePrice": 179,
"currentPrice": 179,
"highPrice": 179,
"lowPrice": 179,
"currentStock": 50,
"toggle": 0
},
{
"subCategoryId": "57fdf5c2c3ed2bd632c06226",
"subCategoryName": "Belvedere",
"basePrice": 179,
"currentPrice": 179,
"highPrice": 179,
"lowPrice": 179,
"currentStock": 50,
"toggle": 0
},
{
"subCategoryId": "57fdf627c3ed2bd632c06229",
"subCategoryName": "Absolut",
"basePrice": 129,
"currentPrice": 129,
"highPrice": 129,
"lowPrice": 129,
"currentStock": 125,
"toggle": 0
},
{
"subCategoryId": "57fdf61fc3ed2bd632c06228",
"subCategoryName": "Finlandia",
"basePrice": 129,
"currentPrice": 129,
"highPrice": 129,
"lowPrice": 129,
"currentStock": 100,
"toggle": 0
},
{
"subCategoryId": "57fdf64cc3ed2bd632c0622a",
"subCategoryName": "Absolut flavoured",
"basePrice": 149,
"currentPrice": 149,
"highPrice": 149,
"lowPrice": 149,
"currentStock": 50,
"toggle": 0
}
]
},
{
"categoryId": "57f22f18c3ed2bd632c061c4",
"categoryName": "SCOTCH WHISKY",
"categoryimageURL": {
"thumbnail": "link",
"original": "link"
},
"priority": 2,
"subCategory": [
{
"subCategoryId": "57fdf163c3ed2bd632c06217",
"subCategoryName": "Blue lable",
"basePrice": 599,
"currentPrice": 599,
"highPrice": 599,
"lowPrice": 599,
"currentStock": 25,
"toggle": 0
},
{
"subCategoryId": "57fdf236c3ed2bd632c06218",
"subCategoryName": "Royal sulte",
"basePrice": 599,
"currentPrice": 599,
"highPrice": 599,
"lowPrice": 599,
"currentStock": 25,
"toggle": 0
},
{
"subCategoryId": "57fdf24ac3ed2bd632c06219",
"subCategoryName": "Chivas 18yr",
"basePrice": 349,
"currentPrice": 349,
"highPrice": 349,
"lowPrice": 349,
"currentStock": 50,
"toggle": 0
},
{
"subCategoryId": "57fdf2bfc3ed2bd632c0621a",
"subCategoryName": "J/W Gold lable",
"basePrice": 299,
"currentPrice": 299,
"highPrice": 299,
"lowPrice": 299,
"currentStock": 50,
"toggle": 0
},
{
"subCategoryId": "57fdf2f9c3ed2bd632c0621d",
"subCategoryName": "Chivas extra",
"basePrice": 299,
"currentPrice": 299,
"highPrice": 299,
"lowPrice": 299,
"currentStock": 50,
"toggle": 0
},
{
"subCategoryId": "57fdf2d7c3ed2bd632c0621b",
"subCategoryName": "Chivas 12yr",
"basePrice": 199,
"currentPrice": 199,
"highPrice": 199,
"lowPrice": 199,
"currentStock": 125,
"toggle": 0
},
{
"subCategoryId": "57fdf2edc3ed2bd632c0621c",
"subCategoryName": "J/W Black lable",
"basePrice": 199,
"currentPrice": 199,
"highPrice": 199,
"lowPrice": 199,
"currentStock": 125,
"toggle": 0
},
{
"subCategoryId": "57fdf32ac3ed2bd632c0621e",
"subCategoryName": "J/W Red lable",
"basePrice": 129,
"currentPrice": 129,
"highPrice": 129,
"lowPrice": 129,
"currentStock": 125,
"toggle": 0
}
]
},
{
"categoryId": "57fdedabc3ed2bd632c06210",
"categoryName": "BEER",
"priority": 3,
"categoryimageURL": {
"thumbnail": "https://s3-us-west-2.amazonaws.com/barsupply/profileThumb_cy7MXfb.png",
"original": "https://s3-us-west-2.amazonaws.com/barsupply/profilePic_cy7MXfb.png"
},
"subCategory": [
{
"subCategoryId": "57fdf942c3ed2bd632c06236",
"subCategoryName": "Hoegaarden",
"basePrice": 299,
"currentPrice": 299,
"highPrice": 299,
"lowPrice": 299,
"currentStock": 100,
"toggle": 0
},
{
"subCategoryId": "57fdf987c3ed2bd632c06237",
"subCategoryName": "Corona",
"basePrice": 299,
"currentPrice": 299,
"highPrice": 299,
"lowPrice": 299,
"currentStock": 100,
"toggle": 0
},
{
"subCategoryId": "57fdf993c3ed2bd632c06238",
"subCategoryName": "Stella",
"basePrice": 299,
"currentPrice": 299,
"highPrice": 299,
"lowPrice": 299,
"currentStock": 50,
"toggle": 0
},
{
"subCategoryId": "57fdf9afc3ed2bd632c06239",
"subCategoryName": "Carlsberg",
"basePrice": 99,
"currentPrice": 99,
"highPrice": 99,
"lowPrice": 99,
"currentStock": 150,
"toggle": 0
},
{
"subCategoryId": "57fdf9b9c3ed2bd632c0623a",
"subCategoryName": "Tuberg",
"basePrice": 79,
"currentPrice": 79,
"highPrice": 79,
"lowPrice": 79,
"currentStock": 125,
"toggle": 0
},
{
"subCategoryId": "57fdf9c5c3ed2bd632c0623b",
"subCategoryName": "Kingfisher ultra",
"basePrice": 99,
"currentPrice": 99,
"highPrice": 99,
"lowPrice": 99,
"currentStock": 125,
"toggle": 0
},
{
"subCategoryId": "57fdf9cec3ed2bd632c0623c",
"subCategoryName": "Kingfisher",
"basePrice": 79,
"currentPrice": 79,
"highPrice": 79,
"lowPrice": 79,
"currentStock": 150,
"toggle": 0
},
{
"subCategoryId": "57fdfa22c3ed2bd632c0623d",
"subCategoryName": "Kotsberg",
"basePrice": 49,
"currentPrice": 49,
"highPrice": 49,
"lowPrice": 49,
"currentStock": 125,
"toggle": 0
}
]
}];
console.log("All subCategory lengths");
console.log(
obj.map(function(n){ return n.subCategory.length})
)
console.log("More than 5");
console.log(
obj.filter(function(n){ return n.subCategory.length > 5})
)
console.log("less than or equal to 5");
console.log(
obj.filter(function(n){ return n.subCategory.length <= 5})
)
&#13;