I want to filter an array of objects in javascript and use it in react application's reducer.Here is my data structure.
[
{
"categoryProducts": [],
"brandCategoryId": 93907,
"categoryName": "Hi",
"parentCategoryName": null,
"productIdList": [
"e306e4dc-2503-4d85-a7a9-0a9c362a1c95",
"39423cb8-9514-4840-9bcb-bb27b9628468"
],
"productList": [
{
"addon": true,
"brandProductId": 576227,
"productName": "Hi1",
"productType": 0,
"parentCategoryId": null,
"isAddon": true,
"productDescription": "",
"productId": "e306e4dc-2503-4d85-a7a9-0a9c362a1c95",
"brandId": "1672",
"sourceId": "Lt",
"oldAddonProductId": null,
"rank": 1,
"maximumQuantity": 1,
"minimumQuantity": 0,
"brandProductSkuList": [
{
"active": null,
"id": 698345,
"productSKUName": "",
"productSKUDescription": null,
"productSKUId": "fdf19138-5fcf",
"productSKUPrice": 0,
"productSKUStandardUnit": 1,
"isActive": null,
"skuIdUpdatedTime": null
}
]
},
{
"addon": true,
"brandProductId": 576228,
"productName": "hie",
"productType": 1,
"parentCategoryId": null,
"isAddon": true,
"productDescription": "",
"productId": "39423cb8-9514-4840-9bcb-bb27b9628468",
"brandId": "1672",
"sourceId": "LT",
"oldAddonProductId": null,
"rank": 1,
"maximumQuantity": 1,
"minimumQuantity": 0,
"brandProductSkuList": [
{
"active": null,
"id": 698346,
"productSKUName": "",
"productSKUDescription": null,
"productSKUId": "c71e96e7-f8ed-4d5e-9e17-845a43b239c9",
"productSKUPrice": 0,
"productSKUStandardUnit": 1,
"isActive": null,
"skuIdUpdatedTime": null
}
]
}
],
"parentCategory": null
},
{
"categoryProducts": [],
"brandCategoryId": 93797,
"categoryName": "Test category",
"parentCategoryName": null,
"productIdList": [],
"productList": [],
"parentCategory": null
},
I want to filter out those category names whose productSKUid is either null or have a length of 36. So I have tried this code,is it correct? Or should I use filter or map function?
case AppConstants.getUnmappedMenuItemsSuccess:
return {
...state,
unmapped: true,
menu_items: state.menu_items.map((item) => {
item.productList.map(product => {
product.brandProductSkuList.map(idCheck => {
if (idCheck.productSKUId == null || idCheck.productSKUId.length == 36) {
return idCheck;
}
});
return product;
})
return item;
})
}
.filter..filter()and.map()do unrelated things. Why are you using.map()when your stated requirement is to filter the array?