gpt4 book ai didi

javascript - 删除数组的所有元素,删除 : true property

转载 作者:行者123 更新时间:2023-11-30 11:13:02 25 4
gpt4 key购买 nike

各位。我有一组 map 对象。是这样的;

[
{
"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) => {...}) 这个语句不减。问题就在这里,但如果有更好的方法来解决这个问题,那将非常有帮助!谢谢!

最佳答案

尝试使用 .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));

关于javascript - 删除数组的所有元素,删除 : true property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52887100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com