[英]How to compare values of properties in test object via “where” using Ramda
我想使用Ramdajs的“ where”函數(請參閱: “ where”-ramda docs )創建一個測試對象,將它們之間的屬性值進行比較。
例:
let pred = R.where({
a: predTestA(...),
b: predTestB(...),
c: predTestC(...)
});
因此,我想寫一個比較a,b和c值的測試方法。
例:
// Test if the value of "a" is higher or equal than all other values
let predTestA = (...) => {
if ((valA >= ValB) || (valA >= ValC)) {
return true
} else {
return false
}
}
問題:
謝謝你的幫助!
莫夫
Ramda沒有提供。 我相信它曾經做過,但很少使用。 這將是很容易的編寫自己的版本, where
做這個,但是:
const myWhere = curry(function (spec, testObj) {
for (var prop in spec) {
if (R.has(prop, spec) && !spec[prop](testObj[prop], testObj)) {
// ^^^^^^^
return false;
}
}
return true;
});
突出顯示的位是與Ramda版本唯一的真正區別。
// Remember the triangle inequality from geometry class?
const triangular = myWhere({
a: (a, t) => a < t.b + t.c,
b: (b, t) => b < t.a + t.c,
c: (c, t) => c < t.a + t.b
});
triangular({a: 3, b: 4, c: 9}); //=> false
triangular({a: 3, b: 6, c: 8}); //=> true
triangular({a: 7, b: 1, c: 5}); //=> false
triangular({a: 2, b: 8, c: 4}); //=> false
triangular({a: 3, b: 4, c: 5}); //=> true
如果您認為這很重要並且應該回到Ramda本身,請隨時提出問題 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.