如何从嵌套数组中提取包含type为text元素的group对象?
问题
需要从给定的嵌套数组中提取所有包含type为text元素的group对象。该数组中同时存在包含text元素的group和其他类型的group,示例数据如下:
[ { "index": 3, "uid": "188960ecb29_00562b0c", "x": 18.651406278454424, "y": 44.14920570161545, "width": 180.14783325004774, "height": 53.336747638012184, "items": [ { "uid": "18895f59b1a_2c5a5c7a", "locked": false, "rotation": 0, "type": "text", "text": [ "abc" ], "x": 154.37927087307924, "y": 0, "width": 25.768562376968507, "height": 20.90412770669292, "sampleTextChanged": true, "fontSize": 15.590551181102365, "fontFamily": "NimbusSansME", "textBold": false, "textItalic": false, "textUnderline": false, "textAlignment": "TEXT_ALIGN_LEFT", "textLetterSpacing": 0, "textLineSpacing": 1, "color": { "red": 0, "green": 0, "blue": 0, "__class__": "RGBAColor", "alpha": 1 }, "placeholderText": [ "Text" ], "isPlaceholderTextActive": false, "translationKey": "", "newPathCalculation": true, "shadow": { "blur": 0, "color": "{\"red\":255,\"green\":255,\"blue\":255,\"transparent\":0}", "coords": { "x": 0, "y": 0 }, "distance": 0, "opacity": 1 }, "index": 0, "originalTextItem": [ "abc" ], "originalXcoords": [ [ 0, 8.25203001968504, 17.47085691437008, 25.768562376968507 ] ] }, { "index": 1, "uid": "1889607cfdf_091e59ca", "x": 0, "y": 32.432619931319266, "width": 22.175427534448822, "height": 20.90412770669292, "items": [ { "uid": "18895ecc7c7_2d5440b6", "locked": false, "rotation": 0, "type": "text", "text": [ "xyz" ], "x": 0, "y": 0, "width": 22.175427534448822, "height": 20.90412770669292, "sampleTextChanged": true, "fontSize": 15.590551181102365, "fontFamily": "NimbusSansME", "textBold": false, "textItalic": false, "textUnderline": false, "textAlignment": "TEXT_ALIGN_LEFT", "textLetterSpacing": 0, "textLineSpacing": 1, "color": { "red": 0, "green": 0, "blue": 0, "__class__": "RGBAColor", "alpha": 1 }, "placeholderText": [ "Text" ], "isPlaceholderTextActive": false, "translationKey": "", "newPathCalculation": true, "shadow": { "blur": 0, "color": "{\"red\":255,\"green\":255,\"blue\":255,\"transparent\":0}", "coords": { "x": 0, "y": 0 }, "distance": 0, "opacity": 1 }, "index": 0, "originalTextItem": [ "xyz" ], "originalXcoords": [ [ 0, 7.54406065452756, 14.95870755413386, 22.175427534448822 ] ] } ], "type": "group", "rotation": 0 }, { "index": 2, "uid": "188960e945c_35ab99fa", "x": 44.108363106593984, "y": 15.56765756703328, "width": 56.72123163199389, "height": 35.17448047647336, "items": [ { "uid": "18896072844_1298562b", "locked": false, "rotation": 0, "type": "text", "text": [ "group" ], "x": 15.567657567033265, "y": 14.270352769780445, "width": 41.15357406496064, "height": 20.90412770669292, "sampleTextChanged": true, "fontSize": 15.590551181102365, "fontFamily": "NimbusSansME", "textBold": false, "textItalic": false, "textUnderline": false, "textAlignment": "TEXT_ALIGN_LEFT", "textLetterSpacing": 0, "textLineSpacing": 1, "color": { "red": 0, "green": 0, "blue": 0, "__class__": "RGBAColor", "alpha": 1 }, "placeholderText": [ "Text" ], "isPlaceholderTextActive": false, "translationKey": "", "newPathCalculation": true, "shadow": { "blur": 0, "color": "{\"red\":255,\"green\":255,\"blue\":255,\"transparent\":0}", "coords": { "x": 0, "y": 0 }, "distance": 0, "opacity": 1 }, "originalTextItem": [ "group" ], "originalXcoords": [ [ 0, 9.013287401574805, 14.342089074803152, 23.241187869094492, 31.9195220226378, 41.15357406496064 ] ], "index": 2 }, { "index": 3, "uid": "188960e5f49_2341c362", "x": 0, "y": 0, "width": 29.803226500984252, "height": 20.90412770669292, "items": [ { "uid": "188958badfe_3a73220b", "locked": false, "rotation": 0, "type": "text", "text": [ "Text" ], "x": 0, "y": 0, "width": 29.803226500984255, "height": 20.90412770669292, "sampleTextChanged": false, "fontSize": 15.590551181102365, "fontFamily": "NimbusSansME", "textBold": false, "textItalic": false, "textUnderline": false, "textAlignment": "TEXT_ALIGN_LEFT", "textLetterSpacing": 0, "textLineSpacing": 1, "color": { "red": 0, "green": 0, "blue": 0, "__class__": "RGBAColor", "alpha": 1 }, "placeholderText": [ "Text" ], "isPlaceholderTextActive": false, "translationKey": "", "newPathCalculation": true, "shadow": { "blur": 0, "color": "{\"red\":255,\"green\":255,\"blue\":255,\"transparent\":0}", "coords": { "x": 0, "y": 0 }, "distance": 0, "opacity": 1 }, "index": 0, "istextCircularMode": false, "originalTextItem": [ "Text" ], "originalXcoords": [ [ 0, 9.119863435039372, 17.60027066929134, 25.1443313238189, 29.803226500984255 ] ] } ], "type": "group", "rotation": 0 } ], "type": "group", "rotation": 0 } ], "type": "group", "rotation": 0 }]
解决方案
可以通过递归遍历数组的方式,检查每个group对象的items中是否包含type为text的元素(包括子group中的text元素),符合条件的group将被收集返回。以下是JavaScript实现代码:
function extractGroupsWithText(arr) { const result = []; function checkItem(item) { // 如果当前是group类型 if (item.type === 'group') { let hasText = false; // 遍历items检查是否有text元素,或者子group里有text for (const subItem of item.items) { if (subItem.type === 'text') { hasText = true; break; } // 递归检查子group if (subItem.type === 'group' && checkItem(subItem)) { hasText = true; break; } } if (hasText) { result.push(item); return true; } } return false; } arr.forEach(item => checkItem(item)); return result; } // 测试示例数据 const sampleData = [/* 放入你的示例数组 */]; const groupsWithText = extractGroupsWithText(sampleData); console.log(groupsWithText);
代码说明
- 递归检查逻辑:定义内部函数
checkItem,用于判断单个元素是否是包含text的group:- 若当前元素是group,遍历其
items数组 - 发现
type为text的元素,标记该group符合条件 - 若遇到子group,递归调用
checkItem检查子group是否包含text
- 若当前元素是group,遍历其
- 收集结果:所有符合条件的group会被加入
result数组,最终返回该数组
运行上述代码后,会得到示例中所有包含text元素的group对象,包括最外层的group、index为1的group、index为2的group以及index为3的子group。
内容的提问来源于stack exchange,提问作者Jahangir khan
相关产品推荐
相关产品推荐

