【问题标题】:Ramda - get array of objects using ids from other arrayRamda - 使用来自其他数组的 id 获取对象数组
【发布时间】:2021-03-30 23:52:53
【问题描述】:

我有两组数据,我想使用第一组从第二组获取对象数组。我试图自己处理它,但我错过了几个步骤。

这是一组要使用的 id:

const idSet = {
  "41": {
    "id": "41"
  },
  "42": {
    "id": "42"
  },
  "43": {
    "id": "43"
  }
}

这是第二组:

const nodes = {
  "3": {
    "nodeCommentId": 3,
    "nodeId": 43,
  },
  "4": {
    "nodeCommentId": 4,
    "nodeId": 41
  },
  "6": {
    "nodeCommentId": 6,
    "nodeId": 42
  },
  "7": {
    "nodeCommentId": 7,
    "nodeId": 44
  },
}

我需要通过 idnodeId 进行搜索,所以我尝试使用类似这样的方法来仅获取第一组的 id:

const ids = R.compose(
  R.values(),
  R.pluck('id')
)(idSet)

我还想出了类似的东西:R.filter(R.compose(R.flip(R.contains)(ids), R.prop('nodeId')), nodes);

但是我有nodeId,它是一个数字而不是一个字符串,而且我需要一个没有键的对象数组。

期望的输出:

[
  {
    nodeCommentId: 3,
    nodeId: 43
  },
  {
    nodeCommentId: 4,
    nodeId: 41
  },
  {
    nodeCommentId: 6,
    nodeId: 42
  }
]

我们将不胜感激。

【问题讨论】:

  • 您要查找的输出是什么?
  • 您可以使用R.map(Number)ids 转换为数字。然后你以后的过滤工作:jsbin.com/nofatayesi/edit?js,console
  • @ScottSauyet 我正在尝试获取一个包含对象 41、42、43 的数组
  • 我怀疑你的意思是[41, 42, 43],所以你能edit 帖子包含所需的输出吗?
  • @ScottSauyet 完成。现在应该很清楚了。

标签: ramda.js


【解决方案1】:

这可能太难用了,但它可能是一个很好的解决方案的开始:

const nodesById = (idSet) => {
  const ids = map (Number, pluck ('id') (values (idSet)))
  return pipe (values, filter (pipe (prop('nodeId'), contains(__, ids))))
}

const idSet = {41: {id: "41"}, 42: {id: "42"}, 43: {id: "43"}}
const nodes = {3: {nodeCommentId: 3, nodeId: 43, }, 4: {nodeCommentId: 4, nodeId: 41}, 6: {nodeCommentId: 6, nodeId: 42}, 7: {nodeCommentId: 7, nodeId: 44}}

console .log (
  nodesById (idSet) (nodes)
)
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
<script>const {map, pluck, values, pipe, filter, prop, contains, __} = R </script>

我敢肯定,只要稍加努力,我们就可以让它完全没有意义,但我怀疑这会有助于提高可读性。

【讨论】:

    【解决方案2】:

    idSet转换为数字数组,然后用户R.innerJoin得到匹配nodeId的项目:

    const { pipe, values, pluck, map, innerJoin, __, curry } = R
    
    const getIds = pipe(values, pluck('id'), map(Number))
    
    const getNodesById = curry((idSet, nodes) => 
      pipe(
        values, 
        innerJoin(
          ({ nodeId }, id) => nodeId === id, 
          __,
          getIds(idSet)
        )
      )(nodes)
    )
    
    const idSet = {41: {id: "41"}, 42: {id: "42"}, 43: {id: "43"}}
    const nodes = {3: {nodeCommentId: 3, nodeId: 43, }, 4: {nodeCommentId: 4, nodeId: 41}, 6: {nodeCommentId: 6, nodeId: 42}, 7: {nodeCommentId: 7, nodeId: 44}}
    
    const result = getNodesById(idSet)(nodes)
    
    console.log(result)
    .as-console-wrapper {max-height: 100% !important; top: 0}
    &lt;script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"&gt;&lt;/script&gt;

    【讨论】:

      猜你喜欢
      • 2020-02-29
      • 2021-11-08
      • 1970-01-01
      • 2023-04-04
      • 2020-02-29
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 2018-10-14
      相关资源
      最近更新 更多