简体   繁体   English

如何使用 ramda 表达式将项目从一个数组推送到另一个数组?

[英]How to push items from an array to another array using ramda expressions?

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.我有一个小问题,我有一个孩子数组,我解析它,从中获取数据并重新格式化并将其推送到另一个数组subMenuContent ,它工作正常,但我需要更好地清理我的代码,我使用 ramda 作为库来为我简化事情。

Here is what I was doing before, simple javascript.这是我之前做的,简单的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.任何帮助将非常感激。

You might be interested in looking at the R.chain function in Ramda.您可能有兴趣查看 Ramda 中的R.chain function。 This is sometimes known as flatMap , because it can be thought of mapping over the data type and then flattening the nested result.这有时被称为flatMap ,因为它可以被认为是映射数据类型,然后展平嵌套的结果。

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. R.chain可以处理许多不同的数据类型,尽管对于 arrays 它有效地映射到数组的每个元素上,为每个元素生成一个数组,然后将它们连接在一起。

In your example, you will want to look at replacing your outermost const subMenuContents = map(...) with const subMenuContents = chain(...) .在您的示例中,您将希望查看用const subMenuContents = chain(...)替换最外层的const subMenuContents = map(...) ...) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM