javascript - 删除具有 delete: true 属性的数组的所有元素
问题描述
大家。我有一组地图对象。就像这样;
[
{
"link": "./1.css",
"url": "http://opdetect.com/x/1.css",
"css": "css only gets text",
"parentCSS": -1,
"childCSSs": [
{
"link": "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css",
"url": "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1.css",
"childCSSs": [],
"delete": false
},
{
"link": "http://opdetect.com/x/1-2.css",
"url": "http://opdetect.com/x/1-2.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1.css",
"childCSSs": [
{
"link": "https://meyerweb.com/eric/tools/css/reset/reset200802.css",
"url": "https://meyerweb.com/eric/tools/css/reset/reset200802.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1-2.css",
"childCSSs": [],
"delete": false
},
{
"link": "http://opdetect.com/x/1-2-2.css",
"url": "http://opdetect.com/x/1-2-2.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1-2.css",
"childCSSs": [
{
"link": "http://opdetect.com/x/1-2-2-1.css",
"url": "http://opdetect.com/x/1-2-2-1.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1-2-2.css",
"childCSSs": [],
"delete": false
}
],
"delete": false
}
],
"delete": false
}
],
"delete": false
},
{
"link": "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css",
"url": "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css",
"css": "css only gets text",
"parentCSS": -1,
"childCSSs": [],
"delete": true
},
{
"link": "http://opdetect.com/x/1-2.css",
"url": "http://opdetect.com/x/1-2.css",
"css": "css only gets text",
"parentCSS": -1,
"childCSSs": [
{
"link": "https://meyerweb.com/eric/tools/css/reset/reset200802.css",
"url":"https://meyerweb.com/eric/tools/css/reset/reset200802.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1-2.css",
"childCSSs": [],
"delete": false
},
{
"link": "http://opdetect.com/x/1-2-2.css",
"url": "http://opdetect.com/x/1-2-2.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1-2.css",
"childCSSs": [
{
"link": "http://opdetect.com/x/1-2-2-1.css",
"url": "http://opdetect.com/x/1-2-2-1.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1-2-2.css",
"childCSSs": [],
"delete": false
}
],
"delete": false
}
],
"delete": true
},
{
"link": "https://meyerweb.com/eric/tools/css/reset/reset200802.css",
"url": "https://meyerweb.com/eric/tools/css/reset/reset200802.css",
"css": "css only gets text",
"parentCSS": -1,
"childCSSs": [],
"delete": true
},
{
"link": "http://opdetect.com/x/1-2-2.css",
"url": "http://opdetect.com/x/1-2-2.css",
"css": "css only gets text",
"parentCSS": -1,
"childCSSs": [
{
"link": "http://opdetect.com/x/1-2-2-1.css",
"url": "http://opdetect.com/x/1-2-2-1.css",
"css": "css only gets text",
"parentCSS": "http://opdetect.com/x/1-2-2.css",
"childCSSs": [],
"delete": false
}
],
"delete": true
},
{
"link": "http://opdetect.com/x/1-2-2-1.css",
"url": "http://opdetect.com/x/1-2-2-1.css",
"css": "css only gets text",
"parentCSS": -1,
"childCSSs": [],
"delete": true
}
]
我想删除所有具有delete: true
数组childCSS: [...]
对象的元素。我怎样才能做到这一点?我试图这样做;
links.forEach((link, index) => {
postTraverse(link, links, index);
});
const postTraverse = (obj, links, index) => {
if(!obj.delete) {
if(obj.hasOwnProperty('childCSSs')) {
if(obj.childCSSs.length > 0) {
obj.childCSSs.forEach((childObj, childIndex) => {
return postTraverse(childObj, links, childIndex);
});
}
}
} else {
if(obj.hasOwnProperty('childCSSs')) {
if(obj.parentCSS == -1) {
links.splice(index, 1);
} else {
obj.childCSSs.splice(index, 1);
}
} else {
links.splice(index, 1);
}
}
};
这种解决问题的方法,由于 after 跳过了一些元素array.splice(index, 1)
,因此数组的长度减少了 1 并且array.forEach((link, index) => {...})
该语句的索引不会减少。问题就在这里,但如果有更好的方法来解决这个问题,它会非常有帮助!谢谢!
解决方案
如果要将项目保留在数组中,请尝试使用.filter
过滤项目属性的 a :childCSSs
const doFilter = arr => arr.filter((item) => {
if (item.delete) return false;
item.childCSSs = doFilter(item.childCSSs);
return true;
});
const input=[{"link":"./1.css","url":"http://opdetect.com/x/1.css","css":"css only gets text","parentCSS":-1,"childCSSs":[{"link":"https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css","url":"https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1.css","childCSSs":[],"delete":!1},{"link":"http://opdetect.com/x/1-2.css","url":"http://opdetect.com/x/1-2.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1.css","childCSSs":[{"link":"https://meyerweb.com/eric/tools/css/reset/reset200802.css","url":"https://meyerweb.com/eric/tools/css/reset/reset200802.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1-2.css","childCSSs":[],"delete":!1},{"link":"http://opdetect.com/x/1-2-2.css","url":"http://opdetect.com/x/1-2-2.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1-2.css","childCSSs":[{"link":"http://opdetect.com/x/1-2-2-1.css","url":"http://opdetect.com/x/1-2-2-1.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1-2-2.css","childCSSs":[],"delete":!1}],"delete":!1}],"delete":!1}],"delete":!1},{"link":"https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css","url":"https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css","css":"css only gets text","parentCSS":-1,"childCSSs":[],"delete":!0},{"link":"http://opdetect.com/x/1-2.css","url":"http://opdetect.com/x/1-2.css","css":"css only gets text","parentCSS":-1,"childCSSs":[{"link":"https://meyerweb.com/eric/tools/css/reset/reset200802.css","url":"https://meyerweb.com/eric/tools/css/reset/reset200802.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1-2.css","childCSSs":[],"delete":!1},{"link":"http://opdetect.com/x/1-2-2.css","url":"http://opdetect.com/x/1-2-2.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1-2.css","childCSSs":[{"link":"http://opdetect.com/x/1-2-2-1.css","url":"http://opdetect.com/x/1-2-2-1.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1-2-2.css","childCSSs":[],"delete":!1}],"delete":!1}],"delete":!0},{"link":"https://meyerweb.com/eric/tools/css/reset/reset200802.css","url":"https://meyerweb.com/eric/tools/css/reset/reset200802.css","css":"css only gets text","parentCSS":-1,"childCSSs":[],"delete":!0},{"link":"http://opdetect.com/x/1-2-2.css","url":"http://opdetect.com/x/1-2-2.css","css":"css only gets text","parentCSS":-1,"childCSSs":[{"link":"http://opdetect.com/x/1-2-2-1.css","url":"http://opdetect.com/x/1-2-2-1.css","css":"css only gets text","parentCSS":"http://opdetect.com/x/1-2-2.css","childCSSs":[],"delete":!1}],"delete":!0},{"link":"http://opdetect.com/x/1-2-2-1.css","url":"http://opdetect.com/x/1-2-2-1.css","css":"css only gets text","parentCSS":-1,"childCSSs":[],"delete":!0}]
console.log(doFilter(input));
推荐阅读
- python - 如何在运行其他代码时运行异步 websocket?
- html - 画布没有完全填满
- ios - GCM 通知未注册令牌且未收到通知
- c++ - OpenCV GAPI 性能不如预期
- logging - 将日志尾随到另一台设备
- react-native - 当 StackNavigator 呈现时,react-navigation 总是挂载初始屏幕(v5)
- c# - 当存储的对象从未被寻址时,二维数组如何改变值?
- database - codeigniter mysql会话cookie问题
- java - 使用 jsch 连接到具有键盘交互式密码身份验证的服务器
- python - 比较真假语句时不理解布尔输出