2016-11-01 2 views
-3

다음과 같은 연관 배열이 있습니다. 하위 카테고리가 5 개 이상일 때마다 2 개 이상의 부분으로 나눠야합니다. 가능합니까? 이 배열을 동적으로 배열을 분할하는 데 자바 스크립트 코드가 필요하므로 한 범주에 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" 

     } 
    ] 
    } 
] 
+1

어떻게 쪼개고 싶니? 예상되는 결과는 무엇입니까? – kukkuz

+0

분할 된 항목에 대해 어떻게해야합니까? –

+0

예상 결과는 –

답변

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

+0

이어야합니다.이 배열을 동적으로 분할하여 하나의 카테고리에 5 개의 하위 카테고리가 있어야하고 카테고리에 5 개 이상의 하위 카테고리가있는 경우 남은 하위 카테고리가있는 요소를 하나 더 만들어야합니다. –

1

5 개 이상의 subcategory 단일 category 항목을 고려 - 필요한 경우가 견인 이상의 요소로 분할 Array.prototype.reduceObject.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;}

+0

@ShifaliGoyal이 (가) 당신의 생각을 알려주세요. 감사! – kukkuz

+0

해답을 가져 주셔서 감사합니다. 그것은 완벽하게 작동합니다. –

+0

@ShifaliGoyal 멋지다, 당신은 대답과 upvote도 받아 들일 수 있니? 감사! – kukkuz