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; };¿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);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!