0

I have a small problem, I have an array of children, I parse it, I get data from it and reformat it and push it into another array subMenuContent, it is working fine, but I need to clean my code a little bit better, I am using ramda as a library to simplify things for me.

Here is what I was doing before, simple javascript.

    let subMenuContent = [];

    for (let counter = 0; counter < children.length; counter++) {
      let parent = children[counter].props.item.id;
      let childs = children[counter].props.item.childrens;
      for (let i = 0; i < childs.length; i++) {
        let child = {
          content: childs[i],
          class: 'cdiscountLinks cdiscountChip-' + childs[i].id + '-' + parent,
        };
        subMenuContent.push(child);
      }
    }

What I am trying to do now is :

    const subMenuContents = map(function(item){
      let parent = item.props.item.id;
      let childs = item.props.item.childrens;
      const itemChilds = map(function(child){
        let childItem = {
          content: child,
          class: 'cdiscountLinks cdiscountChip-' + child.id + '-' + parent,
        }
        return childItem;
      }, childs)
      return itemChilds;

    },children)

EDIT : Now I used this

    const subMenuContents = pipe(
      map(function(item) {
        let parent = item.props.item.id;
        let childs = item.props.item.childrens;
        const itemChilds = map(function(child) {
          let childItem = {
            content: child,
            class: 'cdiscountLinks cdiscountChip-' + child.id + '-' + parent,
          };
          return childItem;
        }, childs);
        return itemChilds;
      }, children),
      flatten,
    )(subMenuContents);

But I get an error :

f.apply is not a function

What am I doing wrong ? Any help would be much appreciated.

1
  • It would help a lot if you could share some simplified input data and the expected output. Commented Jun 22, 2020 at 20:31

1 Answer 1

3

You might be interested in looking at the R.chain function in Ramda. This is sometimes known as flatMap, because it can be thought of mapping over the data type and then flattening the nested result.

R.chain can work with many different data types, though for arrays it effectively maps over each element of the array to produce an array for each then concatenates them all together.

In your example, you will want to look at replacing your outermost const subMenuContents = map(...) with const subMenuContents = chain(...).

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

3 Comments

I used the flatten ramda function, according to the documentation it does what I am looking for, but I got an error, can you check my update ?
If you're using R.pipe then you'll only want to partially apply the first function (i.e. don't feed it the children yet). Then where you currently are passing subMenuContents as an argument, you'll want to replace with children instead.
Or if you're not planning on reusing subMenuContents as a function, you can remove the use of pipe with flatten(map(function(item) { ... }, children)), or more succinctly as chain(function(item) { ... }, children).

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.