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

126
Visualizações
Tener problemas para eliminar un objeto de una matriz donde se encuentra una coincidencia

Tengo la función de seguimiento que eliminará el objeto de la matriz. También devuelve el árbol de matrices sin los elementos que se eliminaron. Mi problema es que funciona cuando mi objToFindBy es nulo y elimina todo lo que se encuentra {group: null}; sin embargo, se produce un error con el rechazo de la promesa si configuro objToFindBy {group: 'some string'}

Este código debe eliminar todas las apariciones en las que objToFindBy coincida, por ejemplo, {group: null} encontrará en todas partes si el grupo es nulo y eliminará todos los objetos y luego devolverá el árbol completo sin los objetos que se eliminaron.

 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

¿Qué tal una solución más corta?

 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

Su función sería mucho más simple, reutilizable, mejor, si no envía el tree redundante, sino deleteFrom(tree.items, "group", null); . Piénsalo.

 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