I am mainly trying to avoid the nested forEach
statements and am trying to move towards a lazy evaluation type solution.
var skus = [{
"id": 25175837,
"giftOptions": 1,
"rmsSkuId": 53750485,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 0,
"colorId": 19219,
"swatchImageUrl": "SwatchSmall/4/_7075044.jpg",
"size": "1X",
"color": "Navy",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175838,
"giftOptions": 1,
"rmsSkuId": 53750486,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075044,
"colorId": 159942,
"swatchImageUrl": "",
"size": "2X",
"color": "Navy",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175839,
"giftOptions": 1,
"rmsSkuId": 53750487,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075044,
"colorId": 159942,
"swatchImageUrl": "",
"size": "3X",
"color": "Navy",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175840,
"giftOptions": 1,
"rmsSkuId": 53750488,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075044,
"colorId": 159942,
"swatchImageUrl": "",
"size": "4X",
"color": "Navy",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175841,
"giftOptions": 1,
"rmsSkuId": 53750489,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075044,
"colorId": 159942,
"swatchImageUrl": "",
"size": "5X",
"color": "Navy",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175843,
"giftOptions": 1,
"rmsSkuId": 53750508,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075040,
"colorId": 118311,
"swatchImageUrl": "SwatchSmall/0/_7075040.jpg",
"size": "1X",
"color": "Rl Black",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175844,
"giftOptions": 1,
"rmsSkuId": 53750509,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075040,
"colorId": 118311,
"swatchImageUrl": "SwatchSmall/0/_7075040.jpg",
"size": "2X",
"color": "Rl Black",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175845,
"giftOptions": 1,
"rmsSkuId": 53750510,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075040,
"colorId": 118311,
"swatchImageUrl": "SwatchSmall/0/_7075040.jpg",
"size": "3X",
"color": "Rl Black",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175846,
"giftOptions": 1,
"rmsSkuId": 53750511,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075040,
"colorId": 118311,
"swatchImageUrl": "SwatchSmall/0/_7075040.jpg",
"size": "4X",
"color": "Rl Black",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}, {
"id": 25175847,
"giftOptions": 1,
"rmsSkuId": 53750512,
"available": 1,
"backOrder": "",
"channelId": 1,
"photoGroupId": 7075040,
"colorId": 118311,
"swatchImageUrl": "SwatchSmall/0/_7075040.jpg",
"size": "5X",
"color": "Rl Black",
"width": null,
"value": "",
"price": "$50.00",
"choiceGroup": "Tall",
"isHolidayAvailable": true,
"isSameDayDeliveryAvailable": true
}];
_(skus)
.groupBy(function (n) {
return n.color;
})
.forEach(function (n, key) {
var firstObj = n[0],
correctColorId = firstObj.colorId;
// is it possible to chain this forEach to the previous one? or some other
// cleaner work around
n.forEach(function (obj, index) {
if (obj.colorId !== correctColorId) {
_.assign(obj, {colorId: correctColorId});
}
});
});
console.log(skus);