Ramda循环遍历数组

时间:2017-10-20 08:46:53

标签: javascript arrays filter reduce ramda.js

循环可能是错误的术语,但它描述了我正在尝试的内容。 我想给平面数据提供结构,但我还需要跟踪它来自的数组。

基本上我的规则是(每个数组):

  1. 如果存在级别1,则为其提供项目的nametypechild数组。每当出现1级时(即使在相同的数组中),它应该创建一个新条目。

  2. typechild内,放置任何级别为> 1的项目

  3. 如果没有等级1,则为该项目的nametypechild数组提供。

  4. 下面我的代码几乎,除了它应该创建一个数组EVERYTIME它看到一个级别1.我的例子是有道理的:

    输入数据

     [
      {
        "title": "Test 1",
        "type": [{
            "name": "Animal",
            "level": 1
          },
          {
            "name": "Food",
            "level": 1
          },
          {
            "name": "Chicken",
            "level": 3
          }
        ]
      },
      {
        "title": "Test 2",
        "type": [{
          "name": "Foo",
          "level": 2
        }]
      }
    ]
    

    注意:动物和食物都是LEVEL 1项目。所以它应该像这样创建两个ARRAYS ......

    所需的输出

    [
        {
            name: "Animal",
            typechild: [
                {
                    level: 2,
                    name: "Chicken"
                }
            ]
        },
        {
            name: "Food",
            typechild: [
                {
                    level: 2,
                    name: "Chicken"
                }
            ]
        },
        {
            name: "NoName",
            typechild: [
                {
                    level: 2,
                    name: "Foo"
                }
            ]
        }
    ]
    

    Ramda尝试(试试这里:https://dpaste.de/JQHw):

    const levelEq = (n) => pipe(prop('level'), equals(n));
    const topLevel = pipe(prop('type'), find(levelEq(1)));
    const topLevelName = pipe(topLevel, propOr('NoName', 'name'));
    const extract2ndLevel = pipe(pluck('type'), flatten, filter(levelEq(2)));
    
    const convert = pipe(
      groupBy(topLevelName),
      map(extract2ndLevel),
      map(uniq),
      toPairs,
      map(zipObj(['name', 'typechild']))
    );
    

1 个答案:

答案 0 :(得分:1)

这样的东西?

var output = [{
  "name": "Animal",
  "typechild": [{
    "name": "Chicken",
    "level": 3
  }, {
    "name": "Dog",
    "level": 2
  }]
}, {
  "name": "Food",
  "typechild": [{
    "name": "Chicken",
    "level": 3
  }]
}, {
  "name": "No name",
  "typechild": [{
    "name": "Foo",
    "level": 2
  }, {
    "name": "Baz",
    "level": 2
  }]
}]


let out = {},
  typechild = {},
  k;

const data = [{
  "title": "Test 1",
  "type": [{
    "name": "Animal",
    "level": 1
  }, {
    "name": "Food",
    "level": 1
  }, {
    "name": "Chicken",
    "level": 3
  }]
}, {
  "title": "Test 2",
  "type": [{
    "name": "Foo",
    "level": 2
  }]
}, {
  "title": "Test 3",
  "type": [{
    "name": "Baz",
    "level": 2
  }]
}, {
  "title": "Test 4",
  "type": [{
    "name": "Animal",
    "level": 1
  }, {
    "name": "Dog",
    "level": 2
  }]
}]


data.forEach((node) => {
  k = false;
  typechild[node.title] = [];
  node.type && node.type.forEach((t, i) => {
    if (t.level == 1) {
      k = true;

      !out[t.name] ? out[t.name] = {
        name: t.name,
        typechild: typechild[node.title]
      } : out[t.name].typechild = out[t.name].typechild.concat(typechild[node.title]);
    } else {
      typechild[node.title].push(t);
    }
    if (i == node.type.length - 1 && !k && typechild[node.title].length) {
      out['No name'] = out['No name'] || {
        name: 'No name',
        typechild: []
      };

      out['No name'].typechild = out['No name'].typechild.concat(typechild[node.title]);
    }
  });
});

console.log(JSON.stringify(Object.values(out)));