Ramda 如何过滤某些键包含的对象数组
Ramda how to filter array of objects where some key contains
如果其中一个键包含这样的字符串,我想过滤一个对象数组。
给定数据:
const data = [
{id: 1, value: 'abs', x: 'ee'}
{id: 2, value: 'ws', x: '21'},
{id: 3, value: 'asd', x: 'as'},
{id: 4, value: 'x', x: 'ee'},
]
如果某些值包含给定的输入,我希望能够给出字符串或数字来过滤此数组
如果我得到 w
我希望能够只得到第二个元素
如果我得到 a
我希望能够得到第一个和第三个元素等等。
提前致谢
你可以这样做:
const data = [
{id: 1, value: 'abs', x: 'ee'},
{id: 2, value: 'ws', x: '21'},
{id: 3, value: 'asd', x: 'as'},
{id: 4, value: 'x', x: 'ee'}
]
const customFilter = val => R.filter(R.compose(R.any(R.contains(val)),R.values))
console.log(customFilter('a')(data))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
如果其中一个键包含这样的字符串,我想过滤一个对象数组。 给定数据:
const data = [
{id: 1, value: 'abs', x: 'ee'}
{id: 2, value: 'ws', x: '21'},
{id: 3, value: 'asd', x: 'as'},
{id: 4, value: 'x', x: 'ee'},
]
如果某些值包含给定的输入,我希望能够给出字符串或数字来过滤此数组
如果我得到 w
我希望能够只得到第二个元素
如果我得到 a
我希望能够得到第一个和第三个元素等等。
提前致谢
你可以这样做:
const data = [
{id: 1, value: 'abs', x: 'ee'},
{id: 2, value: 'ws', x: '21'},
{id: 3, value: 'asd', x: 'as'},
{id: 4, value: 'x', x: 'ee'}
]
const customFilter = val => R.filter(R.compose(R.any(R.contains(val)),R.values))
console.log(customFilter('a')(data))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>