如何使用JavaScript从指定JSON对象中筛选包含至少一个数字的项并生成新JSON?
解决方案:筛选JSON中包含数字的项
我来帮你搞定这个JSON筛选的问题!之前用正则没成功大概率是没处理好嵌套结构的递归遍历——毕竟你的JSON里有多层嵌套的对象和数组,只做表层匹配肯定不行。下面是完整的实现方案:
核心思路
要覆盖整个JSON对象的所有层级,我们需要做递归遍历:
- 对字符串:用正则
/\d/检查是否包含至少一个数字,符合条件才保留 - 对对象/数组:递归处理内部每一项,只有处理后结果非空(有符合条件的子项)才保留整个结构
- 对数字类型:直接保留(本身就是数字,自然符合要求)
- 对空字符串、布尔值、null等其他类型:直接过滤掉
代码实现(JavaScript)
这是可直接运行的代码,能完美处理你的JSON结构:
function filterJsonWithNumbers(obj) { // 处理数组:遍历每个元素,递归过滤后保留非空结果 if (Array.isArray(obj)) { const filteredArray = obj .map(item => filterJsonWithNumbers(item)) .filter(item => { if (item == null) return false; if (typeof item === 'object') { return Array.isArray(item) ? item.length > 0 : Object.keys(item).length > 0; } return true; }); return filteredArray.length > 0 ? filteredArray : undefined; } // 处理对象:遍历每个键值对,递归过滤后保留符合条件的项 if (typeof obj === 'object' && obj !== null) { const filteredObj = {}; for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { const value = obj[key]; const filteredValue = filterJsonWithNumbers(value); // 判断是否保留当前项:要么是过滤后有内容的结构,要么是含数字的字符串/数字 let keep = false; if (filteredValue !== undefined) { keep = true; } else if (typeof value === 'string') { keep = /\d/.test(value); } else if (typeof value === 'number') { keep = true; } if (keep) { filteredObj[key] = filteredValue !== undefined ? filteredValue : value; } } } return Object.keys(filteredObj).length > 0 ? filteredObj : undefined; } // 处理基本类型:字符串检查是否含数字,数字直接保留 if (typeof obj === 'string') { return /\d/.test(obj) ? obj : undefined; } if (typeof obj === 'number') { return obj; } // 其他不符合条件的类型返回undefined,会被过滤 return undefined; } // 测试你的原始JSON const originalJson = { "images": [ { "key": "ASDV1-01.jpg", "image_location": "image1.jpg", "data": { "documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF", "scandate": "Feb 1 2018 12:05PM", "F08": "1", "F09": "", "F11": "" }, "crops": { "F08": { "rectangle": { "left": 690, "top": 2111, "width": 597, "height": 121 } }, "F09": {}, "F10": { "rectangle": { "left": 653, "top": 821, "width": 653, "height": 243 } }, "F11": {} } }, { "key": "ASDV1-01.jpg", "image_location": "image.png", "crops": { "F05": { "rectangle": { "left": 0, "top": 808, "width": 624, "height": 243 } } }, "metadata": [ { "name": "colors", "data": { "dimensions": { "width": 2000, "height": 2600 }, "coordinates": { "width": { "x": { "lat": 4, "long": [12, 345] }, "y": { "lat": { "x" : [12,345], "y": "234" }, "long": 123 } } } } } ] }, { "key": "ASDV1-02.jpg", "image_location": "image.png" } ] }; const filteredResult = filterJsonWithNumbers(originalJson); console.log(JSON.stringify(filteredResult, null, 2));
代码说明
- 递归遍历:不管是数组还是嵌套对象,都会逐层深入处理每一个值
- 严格过滤:空对象、空数组、不含数字的空字符串都会被自动剔除
- 保留有效结构:只要父级下有符合条件的子项,整个父级结构会被保留(比如
crops下的F08因为包含数字坐标,会被完整保留)
最终输出示例
运行代码后,你会得到只包含含数字项的JSON,比如:
{ "images": [ { "key": "ASDV1-01.jpg", "data": { "documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF", "scandate": "Feb 1 2018 12:05PM", "F08": "1" }, "crops": { "F08": { "rectangle": { "left": 690, "top": 2111, "width": 597, "height": 121 } }, "F10": { "rectangle": { "left": 653, "top": 821, "width": 653, "height": 243 } } } }, { "key": "ASDV1-01.jpg", "crops": { "F05": { "rectangle": { "left": 0, "top": 808, "width": 624, "height": 243 } } }, "metadata": [ { "data": { "dimensions": { "width": 2000, "height": 2600 }, "coordinates": { "width": { "x": { "lat": 4, "long": [ 12, 345 ] }, "y": { "lat": { "x": [ 12, 345 ], "y": "234" }, "long": 123 } } } } } ] }, { "key": "ASDV1-02.jpg" } ] }
内容的提问来源于stack exchange,提问作者Luca Petrescu
相关产品推荐
相关产品推荐

