大家。 我有一组地图对象。像这样;
[
{
"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);
}
}
};
此解决问题的方法,跳过了array.splice(index, 1)
之后的某些元素原因,因此数组的长度减小1,array.forEach((link, index) => {...})
的索引此语句不减小。问题在这里,但是如果有解决此问题的更好方法,可能会非常有帮助!谢谢!
答案 0 :(得分:3)
如果要将该项目保留在数组中,请尝试使用.filter
来过滤该项目的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));