我想用_.chain()作为条件。这是房客应该用来做的事吗?
if(_.chain(123).isNumber()
.anotherCheck()
.anotherCheck()
.value() {
// do stuff
}发布于 2016-12-02 01:56:17
你可以做一些像
_.chain(123)
.thru(function(num) {
return _.every([ // or _.some for any item
_.isNumber(num),
_.anotherCheck(num),
_.anotherCheck(num)
]);
})
.value();发布于 2016-12-02 01:03:16
在这种情况下,chain没有意义,因为isNumber将返回一个boolean。所以anotherCheck不会得到这个数字,而是isNumber的结果。
为此使用lodash的一种方法是使用_.every,例如:
function testNumber(num) {
return _.every([_.isNumber(num), num > 100, num % 2 === 0]);
}
function testNumberResult(num) {
var canUse = testNumber(num);
if (canUse) {
console.log(num, 'num is a number greater than 100 and even');
} else {
console.log(num, 'num did not pass tests');
}
}https://stackoverflow.com/questions/40922674
复制相似问题