0

I have two sets of data and I would like to use the first one to get an array of objects from the second one. I tried to deal with it by myself but I am missing few steps.

Here is set of ids to use:

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

And here is second set:

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

I need to search by id and nodeId so I tried to use something like this to get only ids from first set:

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

I also came up with something like: R.filter(R.compose(R.flip(R.contains)(ids), R.prop('nodeId')), nodes);

But then I have nodeId which is a number and not a string plus I need an array of objects without keys.

Desired output:

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

Any help will be appreciated.

6
  • What is the output you're looking for? Commented Mar 29, 2021 at 16:39
  • You can convert ids to numerics with R.map(Number). Then your later filtering works: jsbin.com/nofatayesi/edit?js,console Commented Mar 29, 2021 at 16:41
  • @ScottSauyet I am trying to get an array with objects 41, 42, 43 inside Commented Mar 29, 2021 at 16:47
  • I doubt you just mean [41, 42, 43], so could you edit the post to include the desired output? Commented Mar 29, 2021 at 16:49
  • 1
    @ScottSauyet Done. Now it should be clear. Commented Mar 29, 2021 at 16:53

2 Answers 2

2

This is probably too ugly to use, but it might be a start at a nice solution:

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>

I'm sure that with a little work, we could make this entirely point-free, but I doubt that will help readability.

Sign up to request clarification or add additional context in comments.

Comments

1

Transform the idSet to an array of numbers, and then user R.innerJoin to get the items with the matching 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}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.