Filtering an array of objects using ramda.js

I have an array of objects. I want to filter it to get objects in which some property contains a math string.

If my array

 var data = [
 {"name: "John",
 "surname": "Smith"},
 {"name": "Peter",
 "surname: "Smithie"}]

I and the filter with the string "Media", it should return both elements. If the string is "John", only the first.

This is my code:

var filtered = R.filter(R.where({ x: R.contains("Smi")}))(data);

I get an error message:

Cannot read property 'indexOf' of undefined

Can someone help me with my Ramda function? It must be something small, I’m probably missing out. thanks in advance

+4
source share
2 answers

I cannot answer in Ramda, but if you want to implement the same functionality in JS, you can easily do the following:

var   data = [{"name": "John", "surname": "Smith"}, {"name": "Peter", "surname": "Smirnof"}],
getObjects = (d,f) => d.filter(o => Object.keys(o).some(k => o[k].includes(f)));
console.log(getObjects(data,"Smi"));
console.log(getObjects(data,"Jo"));
Run codeHide result
+1
source

- :

R.filter(R.pipe(R.values, R.any(R.contains('Smi'))))(data)

contains, , . .

+1

Source: https://habr.com/ru/post/1655847/


All Articles