0

I am trying to get outgoingNodes IDs which are stored in array which is inside objects like in example below but I have no idea where to start...:

const nodes = {
    "818": {
        "id": "818",
        "index": 1,
        "outgoingNodes": [
            "819"
        ],
    },
    "819": {
        "id": "819",
        "outgoingNodes": [
            "820",
            "821"
        ],
    }
}

I would like to get an array of IDs as a result. Any help will be appreciated.

1 Answer 1

2

Get the values (sub objects), pluck the outgoingNodes arrays, and flatten to a single array:

const { pipe, values, pluck, flatten } = R

const fn = pipe(
  values,
  pluck('outgoingNodes'),
  flatten
)

const nodes = {"818":{"id":"818","index":1,"outgoingNodes":["819"]},"819":{"id":"819","outgoingNodes":["820","821"]}}

const result = fn(nodes)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>

Another option is to combine getting the outgoingNodes arrays, and flattening to a single array using R.chain with R.prop:

const { pipe, values, chain, prop } = R

const fn = pipe(
  values,
  chain(prop('outgoingNodes')),
)

const nodes = {"818":{"id":"818","index":1,"outgoingNodes":["819"]},"819":{"id":"819","outgoingNodes":["820","821"]}}

const result = fn(nodes)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>

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

1 Comment

Thank you for help :) This time I was not even close to the proper solution so you saved me a lot of time

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.