繁体   English   中英

迭代一个Json对象并存储最终结果

[英]Iterating a Json object and storing the end result

我正在尝试迭代一个类别数组的JSON对象。类别可能有也可能没有子类别。

如果hasSubcategory = false ,则类别ID应存储在最终数组中。

如果hasSubcategory = true ,则应迭代直到子类别变为hasSubcategory = false并将ID存储在最终数组中。

parentId字段代表类别的父类别。 更重要的是,可能有一个子类别的子类别

最终数组应仅具有hasSubcategory = false的类别ID。

首先Filter以获取所需的对象,然后map它们map到所需的模式。

 let arr = [{"_id":"1","name":"DESKTOP COMPUTERS","hasSubCategory":"true","parentId":"0"},{"_id":"2","name":"LAPTOP COMPUTERS","hasSubCategory":"false","parentId":"1"},{"_id":"3","name":"MONITORS","hasSubCategory":"false","parentId":"2"},{"_id":"4","name":"INPUT DEVICES","hasSubCategory":"true","parentId":"0"},{"_id":"5","name":"PRINTERS SCANNERS","hasSubCategory":"false","parentId":"4"},{"_id":"6","name":"ACCESSORIES","hasSubCategory":"false","parentId":"4"}]; res = arr.filter(v => v.hasSubCategory === "false").map(e => Number(e._id)) console.log(res) // [ 2, 3, 5, 6 ] 

或者,您也可以使用reduce

 let arr = [{"_id":"1","name":"DESKTOP COMPUTERS","hasSubCategory":"true","parentId":"0"},{"_id":"2","name":"LAPTOP COMPUTERS","hasSubCategory":"false","parentId":"1"},{"_id":"3","name":"MONITORS","hasSubCategory":"false","parentId":"2"},{"_id":"4","name":"INPUT DEVICES","hasSubCategory":"true","parentId":"0"},{"_id":"5","name":"PRINTERS SCANNERS","hasSubCategory":"false","parentId":"4"},{"_id":"6","name":"ACCESSORIES","hasSubCategory":"false","parentId":"4"}]; res = arr.reduce((a,c) => {if (c.hasSubCategory === "false") a.push(Number(c._id)); return a;}, []) console.log(res) 

我建议链式过滤器 + 映射方法:

 const arr = [ { "_id": "1", "name": "DESKTOP COMPUTERS", "hasSubCategory": "true", "parentId": "0" }, { "_id": "2", "name": "LAPTOP COMPUTERS", "hasSubCategory": "false", "parentId": "1" }, { "_id": "3", "name": "MONITORS", "hasSubCategory": "false", "parentId": "2" }, { "_id": "4", "name": "INPUT DEVICES", "hasSubCategory": "true", "parentId": "0" }, { "_id": "5", "name": "PRINTERS SCANNERS", "hasSubCategory": "false", "parentId": "4" }, { "_id": "6", "name": "ACCESSORIES", "hasSubCategory": "false", "parentId": "4" }, ]; const result = arr.filter(el => el.hasSubCategory === 'false').map(el => el._id); console.log(result); 

这真的很简单-只需使用filter提取hasSubCategoryfalse的项目,然后map _id

 const arr = [{"_id":"1","name":"DESKTOP COMPUTERS","hasSubCategory":"true","parentId":"0"},{"_id":"2","name":"LAPTOP COMPUTERS","hasSubCategory":"false","parentId":"1"},{"_id":"3","name":"MONITORS","hasSubCategory":"false","parentId":"2"},{"_id":"4","name":"INPUT DEVICES","hasSubCategory":"true","parentId":"0"},{"_id":"5","name":"PRINTERS SCANNERS","hasSubCategory":"false","parentId":"4"},{"_id":"6","name":"ACCESSORIES","hasSubCategory":"false","parentId":"4"}]; const res = arr.filter(({ hasSubCategory }) => hasSubCategory == "false").map(({ _id }) => _id); console.log(res); 

为了获得更有效的解决方案,请使用reduce

 const arr = [{"_id":"1","name":"DESKTOP COMPUTERS","hasSubCategory":"true","parentId":"0"},{"_id":"2","name":"LAPTOP COMPUTERS","hasSubCategory":"false","parentId":"1"},{"_id":"3","name":"MONITORS","hasSubCategory":"false","parentId":"2"},{"_id":"4","name":"INPUT DEVICES","hasSubCategory":"true","parentId":"0"},{"_id":"5","name":"PRINTERS SCANNERS","hasSubCategory":"false","parentId":"4"},{"_id":"6","name":"ACCESSORIES","hasSubCategory":"false","parentId":"4"}]; const res = arr.reduce((a, { hasSubCategory, _id }) => (hasSubCategory == "false" ? a.push(_id) : a, a), []); console.log(res); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM