简体   繁体   English

使用Ramda对JSON中的JSON对象进行通用过滤

[英]generic filtering of JSON objects in js using Ramda

I like the implementation to be as generic and functional (as in functional programming) as possible, but generally speaking, i'm expecting a json objected with the following structure: 我希望实现尽可能地具有通用性和功能性(就像在函数式编程中一样),但总的来说,我期望一个具有以下结构的json对象:

[
 {
   id: number,
   prop1: string,
   prop2: number,
   prop3: string,
   ...,
   propN: string
 },
 ...
]

(basically, an array of object that contain N properties, some mapped to strings and others to numbers) (基本上是包含N个属性的对象数组,其中一些映射到字符串,而另一些映射到数字)

I am trying to implement a generic set of functions so that i'll be able to achieve something to this end: 我正在尝试实现一组通用功能,以便为此能够实现一些目标:

var filteredResult = filter(by(property, value, lt\gt\eq\contains), collection);

basically, I'd like to return an array with the same object structure, filtered by a property string that I pass into by(), along with the value (either a string or a number) and the type of comparison i'd like to perform. 基本上,我想返回一个具有相同对象结构的数组,该数组由传递给by()的属性字符串以及值(字符串或数字)和我想要的比较类型进行过滤去表演。

generally speaking, for numbers I'd like to be able to filter results where property values are greater/lessthan/in range of the value I pass, with with strings, or arrays of strings, I'd like to find out if the property value contains the value I pass into by(). 一般来说,对于数字,我希望能够在属性值大于/小于/在我传递的值范围内(带有字符串或字符串数​​组)的情况下过滤结果,我想确定该属性是否值包含我传递给by()的值。

Since I'm new to FP, i'm struggling with formatting my code to take advantage of the auto-currying Ramda provides and I'm having trouble composing the different functions while passing the arguments I want. 因为我是FP的新手,所以我一直在努力格式化代码以利用自动循环的Ramda提供的优势,并且在传递所需参数的同时编写各种函数时遇到了麻烦。

For example, I've written this function: 例如,我编写了以下函数:

var byProperty = function(p) {return R.useWith(R.filter, R.propEq(p), R.identity)};

but when I try to use it like so: 但是当我尝试像这样使用它时:

var property = 'prop1', value = 15;
console.log( byProperty( property, value, collection ) );

I get a function instead of the filtered array. 我得到了一个函数,而不是过滤后的数组。

I know I'm missing something trivial here, but it's been kind of hard for me to wrap my head around the way values and functions are passed around in Ramda. 我知道我在这里遗漏了一些琐碎的事情,但是对于在Ramda中传递值和函数的方式来说,让我有些难为情。

but when I try to use it like console.log( byProperty( property, value, collection ) ) I get a function instead of the filtered array. 但是当我尝试像console.log( byProperty( property, value, collection ) )一样使用它时console.log( byProperty( property, value, collection ) )我得到了一个函数,而不是过滤后的数组。

Yes, because your function takes only a single parameter, and returns a function. 是的,因为您的函数只接受一个参数,然后返回一个函数。 You could invoke it like this: 您可以这样调用它:

console.log( byProperty(property)(value, collection) );

but that's probably not what you want. 但这可能不是您想要的。 Also, I think useWith is the wrong tool here, you just want a compose : 另外,我认为useWith是错误的工具,您只需要compose

var byProperty = R.compose(R.filter, R.propEq);

though that still would need to be called like 尽管那仍然需要像

console.log( byProperty(property, value)(collection) );

There is an open issue for this, but currying and composing variadic functions is not trivial. 对此有一个未解决的问题 ,但是,可变参数函数的泛化和组合并非无关紧要。 The best you'll get is probably 最好的可能是

var byProperty = R.curryN(3, function(p, v, c) { return R.filter(R.propEq(p, v), c); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM