0

I would like to filter an array of objects if one of the keys contains so string. given the data:

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'},
]

I want to be able given the sting or number to filter this array if some value contains the given input if i get w i want to be able to get only the second element if i get a i want to be able to get the first and third element and so on.

thanks ahead

tubu13
  • 924
  • 2
  • 17
  • 34
  • Is using Ramda necessary? It would be trivial in standard JS – CertainPerformance Jun 10 '18 at 07:54
  • @CertainPerformance pref to.. but how trivial in standart js? – tubu13 Jun 10 '18 at 07:55
  • @baao this is not what im looking for because you are filtering only be the key `value` i want to be able to filter by all keys – tubu13 Jun 10 '18 at 07:56
  • Changed the comment accordingly, please make sure to specify your requirements in the question when you ask. Your question reads _if some value_ which was confusing as your objects contain a property `value` – baao Jun 10 '18 at 07:57
  • 1
    `data.filter(e => Object.values(e).some(v => String(v).includes('a')))` – baao Jun 10 '18 at 08:01

1 Answers1

2

You can do it like this:

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>
Håken Lid
  • 22,318
  • 9
  • 52
  • 67