Ramda过滤器 - 数字包含数字

时间:2017-11-07 19:10:12

标签: javascript ramda.js

我正在尝试使用ramda进行过滤。除了通过用户输入过滤数字外,一切正常。它只适用于字符串。如何在过滤时告诉ramda将数字值转换为字符串?

const data = [
  {
    col: 123,
    foo: 'sfdfds'
  },
  {
    col: 624,
    foo: 'dsfsdd'
  },
  {
    col: '98523',
    foo: 'sdfsdf'
  }
];

const searchedValue = 23;

const filteredData = R.filter(R.where({ col: R.contains(searchedValue) }))(data)

console.log(filteredData)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

此处的完整代码:https://jsfiddle.net/1qwnem2u/1/

1 个答案:

答案 0 :(得分:1)

Ramda&#39; contains并不意味着像这样工作。但您可以轻松添加自己的:

&#13;
&#13;
const data = [
  {col: 123, foo: 'sfdfds'},
  {col: 624, foo: 'dsfsdd'},
  {col: '98523', foo: 'sdfsdf'}
];

const searchedValue = 23;

const stringContains = R.curry(
  (needle, haystack) =>  R.contains(String(needle), String(haystack))
)

const filteredData = R.filter(R.where(
  { col: stringContains(searchedValue) }
))(data)

console.log(filteredData)
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
&#13;
&#13;
&#13;