根据特定的对象条目重新选择选择器

时间:2018-09-07 06:24:35

标签: javascript selector reselect

假设我具有这样的数据结构:

"data": {
  "1": {
    "id": "1",
    "type": "Parent",
    "params": {...},
    "children": [ 10, 11 ]
  },
  "2": {
    "id": "2",
    "type": "Parent",
    "params": {...},
    "children": [ 12 ]
  },
  "10": {
    "id": "10",
    "type": "Child",
    "params": {...},
    "children": [ ]
  },
  "11": {
    "id": "11",
    "type": "Child",
    "params": {...},
    "children": [ ]
  }
  "12": {
    "id": "12",
    "type": "Child",
    "params": {...},
    "children": [ ]
  }
}

现在我需要创建一个选择器,该选择器将用项目替换子代ID。
我可以做这样的事情:

createSelector(
   (data, parent) => _.pick(data, item => item.type === "Child"),
   (data, parent) => parent.children
   (allChildren, parentChildren) => parentChildren.map(item => allChildren[item])
)

问题在于,每次更改数据结构中的任何条目时,都会重新计算选择器。
我可以让选择器仅依赖于子类型元素的更改吗?

0 个答案:

没有答案