Удалить все элементы массива, который имеет свойство delete: true

1

все. У меня есть массив объектов карты. Это вот так;

[
    {
        "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) => {...}) этот оператор делает не уменьшается. Проблема здесь, но если есть лучший способ решить эту проблему, это может быть очень полезно! Спасибо!

Теги:
arrays
recursion

1 ответ

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));
  • 0
    Мужчина! Ты шутка ?? Это хорошо работает! Большое спасибо, я работал над этой проблемой 2 дня, но вы сделали это с 5 строками кода :) Желаю хорошей жизни!

Ещё вопросы

Сообщество Overcoder
Наверх
Меню