Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Having issues with deleting object from an array where match is found

I have the follow function that will delete object from array. It also returns the array tree without the items that was deleted. My issues is that it works when my objToFindBy is null deleting everything where {group: null} is found however it error with promise rejection if I set objToFindBy {group: 'some string'}

This code should delete all occurrences where the objToFindBy is a match, example {group: null} will find everywhere will the group is null and delete all object and then return the full tree without the objects that was deleted

findAndDeleteAll(tree, 'items', {group: null}) // work and delete all where match. then returns the tree without deleted objects

findAndDeleteAll(tree, 'items', {group: 'd575c91f-4765-4073-a948-5e305116610c'}) // promise rejection
const tree ={
    "type": "app",
    "info": "Custom Layout",
    "items": [
      {
        "id": "d575c91f-4765-4073-a948-5e305116610c",
        "title": "Fc",
        "group": null
      },
      {
        "id": "890d5a1e-3f03-42cd-a695-64a17b6b9bea",
        "title": null,
        "group": null
      },
      {
        "id": "cbe00537-0bb8-4837-8019-de48cb04edd6",
        "title": null,
        "group": "d575c91f-4765-4073-a948-5e305116610c",
      },
      {
        "id": "b8751c32-2121-4907-a229-95e3e49bcb39",
        "title": null,
        "group": "d575c91f-4765-4073-a948-5e305116610c"
      }
    ],
    "Children": []
  }
var findAndDeleteAll = function findAndDeleteAll(tree, childrenKey, objToFindBy) {
      var treeModified = false;
      var findKeys = Object.keys(objToFindBy);
      var findSuccess = false;
      findKeys.forEach(function (key) {
        (0, _lodash2.default)(tree[key], objToFindBy[key]) ? findSuccess = true : findSuccess = false;
      });
      if (findSuccess) {
        Object.keys(tree).forEach(function (key) {
          return delete tree[key];
        });
        return tree;
      }
      function innerFunc(tree, childrenKey, objToFindBy) {
        if (tree[childrenKey]) {
          var _loop = function _loop(index) {
            var findKeys = Object.keys(objToFindBy);
            var findSuccess = false;
            findKeys.forEach(function (key) {
              (0, _lodash2.default)(tree[childrenKey][index][key], objToFindBy[key]) ? findSuccess = true : findSuccess = false;
            });
            if (findSuccess) {
              tree[childrenKey].splice(index, 1);
              treeModified = true;
            }
            if (tree[childrenKey][index].hasOwnProperty(childrenKey)) {
              innerFunc(tree[childrenKey][index], childrenKey, objToFindBy);
            }
          };
    
          for (var index = tree[childrenKey].length - 1; index >= 0; index--) {
            _loop(index);
          }
        }
      }
      innerFunc(tree, childrenKey, objToFindBy);
      return treeModified ? tree : false;
    };
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

how about shorter solution?

const findAndDeleteAll = (tree, childrenKey, nestedKey, nestedValue) => {
  return{...tree, [childrenKey]: tree[childrenKey].filter((row) => {
    return row[nestedKey] !== nestedValue;
  })}
}
const a = findAndDeleteAll(tree, 'items', 'group', null) // work and delete all where match. then returns the tree without deleted objects

const b = findAndDeleteAll(tree, 'items', 'group', 'd575c91f-4765-4073-a948-5e305116610c') // promise rejection

console.warn(a);
console.warn(b);
over 4 years ago · Santiago Trujillo Relatório

0

Your function would be so much simper, reusable, better, if you send not the redundant tree - but instead deleteFrom(tree.items, "group", null);. think about it.

const deleteFrom = (arr, pr, val) => arr.filter(ob => ob[pr] !== val);

const tree = {
  type: "app",
  info: "Custom Layout",
  items: [
    { id: "10c", title: "Fc", group: null  },
    { id: "bea", title: null, group: null  },
    { id: "dd6", title: null, group: "10c" },
    { id: "b39", title: null, group: "10c" },
  ],
  Children: []
};

const items = deleteFrom(tree.items, "group", null);

console.log(items); // Only the filtered items array

const newTree = {...tree, items};

console.log(newTree); // Your brand new tree!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda