我想在 JavaScript 中过滤对象数组并在 React 应用程序的 reducer 中使用它。这是我的数据结构。
[
{
"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
},
<小时/>
我想过滤掉那些productSKUid为空或长度为36的类别名称。所以我尝试了这段代码,是否正确?或者我应该使用过滤器或 map 功能?
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(),则可以使用
返回 Array.prototype.filter()
返回一个或多个数组元素,或者不返回数组元素作为新数组的元素true
或 false
menu_items: state.menu_items.filter(({productList}) => {
let list = productList.find(({brandProductSkuList}) =>
brandProductSkuList);
return list && list.brandProductSkuList.some(({productSKUId}) =>
productSKUId === null || productSKUId.length === 36)
})
var result = [{
"categoryProducts": [],
"brandCategoryId": 93799,
"categoryName": "Test category 3", //1st object
"parentCategoryName": null,
"productIdList": [
"12bcd765-c2d9-40b9-8a88-ab6386a205a9"
],
"productList": [{
"addon": false,
"brandProductId": 576129,
"productName": "test product",
"productType": 2,
"parentCategoryId": null,
"isAddon": false,
"productDescription": "test product description",
"productId": "12bcd765-c2d9-40b9-8a88-ab6386a205a9",
"brandId": "1672",
"sourceId": "LT",
"oldAddonProductId": null,
"rank": 1,
"maximumQuantity": 0,
"minimumQuantity": 0,
"brandProductSkuList": [{
"active": null,
"id": 698174,
"productSKUName": "",
"productSKUDescription": null,
"productSKUId": "trdsfsdf1111",
"productSKUPrice": 12,
"productSKUStandardUnit": 1,
"isActive": null
}]
}],
"parentCategory": null
}, {
"categoryProducts": [],
"brandCategoryId": 93797,
"categoryName": "Test category", //2nd object
"parentCategoryName": null,
"productIdList": [],
"productList": [],
"parentCategory": null
}, {
"categoryProducts": [],
"brandCategoryId": 93775,
"categoryName": "abhishek", //3rd object
"parentCategoryName": null,
"productIdList": [
"510d0acf-8c20-45f5-8b69-0a1aeac3b2ca"
],
"productList": [{
"addon": false,
"brandProductId": 575971,
"productName": "qA2",
"productType": 0,
"parentCategoryId": null,
"isAddon": false,
"productDescription": "sa",
"productId": "510d0acf-8c20-45f5-8b69-0a1aeac3b2ca",
"brandId": "1672",
"sourceId": "LT",
"oldAddonProductId": null,
"rank": 1,
"maximumQuantity": 0,
"minimumQuantity": 0,
"brandProductSkuList": [{
"active": null,
"id": 697967,
"productSKUName": "",
"productSKUDescription": null,
"productSKUId": "b6f38902-2bd9-4595-b1d5-d45db0a30242",
"productSKUPrice": 0,
"productSKUStandardUnit": 1,
"isActive": null
}]
}],
"parentCategory": null
}, ];
const x = result.filter(({productList}) => {
let list = productList.find(({brandProductSkuList}) =>
brandProductSkuList
);
return list && list.brandProductSkuList.some(({productSKUId}) =>
productSKUId === null || productSKUId.length === 36)
});
document.getElementById('id').innerHTML = JSON.stringify(x, null, 2);
<pre id="id">
</pre>
jsfiddle https://jsfiddle.net/3eLh3Lqe/3/
关于javascript - 在javascript中过滤对象数组并在reducer中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45293186/