首页 > 解决方案 > 具有条件的嵌套数组的减速器函数

问题描述

我正在努力为我拥有的嵌套对象编写一个 reducer 函数。

{
    "queryResult": [{
        "__typename": "Parent",
        "_id": "ABC",
        "items": [{
            "__typename": "Child",
            "_id": "123",
            "subitems": [{
                "__typename": "Grandchild",
                "_id": "abc",
            }, {
                "__typename": "Grandchild",
                "_id": "def",
            }, {
                "__typename": "Grandchild",
                "_id": "ghi",
            }, {
                "__typename": "Grandchild",
                "_id": "jkl",
            }, {
                "__typename": "Grandchild",
                "_id": "mno",
            }, {
                "__typename": "Grandchild",
                "_id": "pqr",
            }]
        }, {
            "__typename": "Child",
            "_id": "456",
            "subitems": [{
                "__typename": "Grandchild",
                "_id": "aaa",
            }, {
                "__typename": "Grandchild",
                "_id": "bbb",
            }, {
                "__typename": "Grandchild",
                "_id": "ccc",
            }, {
                "__typename": "Grandchild",
                "_id": "ddd",
            }]
        }, {
            "__typename": "Child",
            "_id": "789",
            "subitems": [{
                "__typename": "Grandchild",
                "_id": "eee",
            }]
        }]
    }]
}

queryResult可以有很多Parents。每个Parent人都有items,每个item人都有subitems 和他们.id的 s。

如何编写一个reducer,queryResult通过接收parentIditemId返回s的数组来减少subitems?例如,对于parentId=ABCitemId=456我需要如下所示的结果:

 "subitems": [{
  "__typename": "Grandchild",
  "_id": "aaa",
}, {
  "__typename": "Grandchild",
  "_id": "bbb",
}, {
  "__typename": "Grandchild",
  "_id": "ccc",
}, {
  "__typename": "Grandchild",
  "_id": "ddd",
}]

注意:所有的 ID 都是随机的,没有任何逻辑。

标签: javascript

解决方案


const sample = {
  "queryResult": [{
    "__typename": "Parent",
    "_id": "ABC",
    "items": [{
      "__typename": "Child",
      "_id": "123",
      "subitems": [{
        "__typename": "Grandchild",
        "_id": "abc",
      }, {
        "__typename": "Grandchild",
        "_id": "def",
      }, {
        "__typename": "Grandchild",
        "_id": "ghi",
      }, {
        "__typename": "Grandchild",
        "_id": "jkl",
      }, {
        "__typename": "Grandchild",
        "_id": "mno",
      }, {
        "__typename": "Grandchild",
        "_id": "pqr",
      }]
    }, {
      "__typename": "Child",
      "_id": "456",
      "subitems": [{
        "__typename": "Grandchild",
        "_id": "aaa",
      }, {
        "__typename": "Grandchild",
        "_id": "bbb",
      }, {
        "__typename": "Grandchild",
        "_id": "ccc",
      }, {
        "__typename": "Grandchild",
        "_id": "ddd",
      }]
    }, {
      "__typename": "Child",
      "_id": "789",
      "subitems": [{
        "__typename": "Grandchild",
        "_id": "eee",
      }]
    }]
  }]
};

function getSubItemsByParentAndItemId(arr, parentId, itemId) {
  return ((arr.find(parentItem =>

    parentItem.__typename === 'Parent'
    && parentItem._id === parentId

  ) || { items: [] }).items.find(childItem =>

    childItem.__typename === 'Child'
    && childItem._id === itemId

  ) || { subitems: [] }).subitems;
}

console.log(
  getSubItemsByParentAndItemId(sample.queryResult, 'ABC', '456')
);
console.log(
  getSubItemsByParentAndItemId(sample.queryResult, 'XYZ')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }


推荐阅读