为什么我的R.flatten(R.map(R.prop返回和未定义的对象在这里?RamdaJS

时间:2016-09-30 16:18:40

标签: javascript ramda.js

https://codepen.io/leongaban/pen/EgvBpx

据我了解,我只是将我的comboTags数组传入。数据总是存在于Ramda语句的右侧。

我希望flatTags包含tag1tag2

的数组

enter image description here

const comboTags = [
  {
    tags: [
      {
        name: 'tag1'
      }
    ],
    person: {
      name: 'Jerry'
    }
  },
  {
    tags: [
      {
        name: 'tag2'
      }
    ],
    person: {
      name: 'Laura'
    }
  }
];

const flattenTags = container => R.flatten(R.map(R.prop('tags')));

let flatTags = flattenTags(comboTags);
console.log('flatTags',flatTags);

1 个答案:

答案 0 :(得分:1)

我相信您正在寻找pluck并注意使用compose或者使用pipe!这允许无点函数:Try It Out

const comboTags = [
  {
    tags: [
      {
        name: 'tag1'
      }
    ],
    person: {
      name: 'Jerry'
    }
  },
  {
    tags: [
      {
        name: 'tag2'
      }
    ],
    person: {
      name: 'Laura'
    }
  }
];

const getTagNames = R.compose(R.pluck('name'), R.flatten, R.pluck('tags'))

getTagNames(comboTags) // ['tag1', 'tag2']