在寻找匹配项并删除数组中的对象时遇到问题。

3
我有下列函数,它会从一个数组中删除对象。同时返回删除后的树形数组。当 objToFindBy 为 null 时,它能正常工作,删除所有 {group: null} 的项。但如果我设置 objToFindBy 为 {group: 'some string'},它就会出现 promise 拒绝的错误。
这段代码应该删除所有与 objToFindBy 匹配的项,例如 {group: null} 将找到所有组为空的地方并删除所有对象,然后返回没有被删除的完整树形数组。
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;
    };

1
如果您发送不必要的 tree 而是 findAndDeleteAll(tree.items, "group", null);,那么您的函数将会更加简单、可重用和优秀。请考虑一下。 - Roko C. Buljan
2个回答

3

有没有更简短的解决方案?

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);

这个解决方案适用于深度嵌套的对象还是只适用于浅层对象? - Jerry seigle
目前它并不是很深,但你可以使用相同的概念进行修改。 - Jafar Jabr

1

如果您发送的不是冗余的tree而是deleteFrom(tree.items, "group", null);,那么您的功能将会更加简单、可重用和优秀。请仔细思考。

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!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接