2016-11-01 6 views
-3

私は以下のような連想配列を持っています。サブカテゴリが5以上のときは、2つ以上の部分に分割する必要がありますか? この配列を分割するにはJavaScriptコードが必要です 1つのカテゴリには5つのサブカテゴリしかなく、カテゴリに5つ以上のサブカテゴリがある場合は、残りのサブカテゴリと同じ名前のカテゴリをもう1つ作成する必要があります。分割連想配列

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

であるはずです。この配列を動的に分割して1つのカテゴリに5つのサブカテゴリしか持たないようにする必要があります。また、カテゴリに5つ以上のサブカテゴリがある場合、残りのサブカテゴリを持つ要素を1つ追加する必要があります。 –

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