简体   繁体   English

如何通过循环创建表达式和逻辑运算符的循环 - javaScript?

[英]How create a loop of expression and logical operators by loop - javaScript?

If I have An array of value and I want to create logical operators while I do a loop over these values如果我有一个值数组并且我想在对这些值进行循环时创建逻辑运算符

const values = [{from: 2, to: 8}, {from: 11, to:20} ..... ]

to get a result like this得到这样的结果

return (8 > 2) || (20 > 11) || .......

How I can do this with javascript if I want to create something complex like如果我想创建一些复杂的东西,我怎么能用 javascript 做到这一点

   const time = [{ from: '2021-07-30', to: '2021-07-20' }, { from: '2021-07-05', to: '2021-07-02' }, ......]
    const disableDates = (date) => {

        time.forEach(element => {
            return (moment(date).isSameOrBefore('2021-07-30') && moment(date).isSameOrAfter('2021-07-20')) || (moment(date).isSameOrBefore('2021-08-05') && moment(date).isSameOrAfter('2021-08-02')) || .....

        });

    }

I want to create this line我想创建这条线

return (moment(date).isSameOrBefore('2021-07-30') && moment(date).isSameOrAfter('2021-07-20')) || (moment(date).isSameOrBefore('2021-08-05') && moment(date).isSameOrAfter('2021-08-02')) || .....

You can use Array.some()您可以使用Array.some()

 const values = [{from: 2, to: 8}, {from: 11, to:20}]; console.log(values.some(x => x.to > x.from));

EDIT Base on new spec.编辑基于新规范。 Using moment.isBetween使用moment.isBetween

 const time = [{ from: '2021-07-30', to: '2021-07-20' }, { from: '2021-07-05', to: '2021-07-02' }]; const disableDates = (testDate) => time.some(x => moment(testDate).isBetween(x.to, x.from, undefined, '[]')); console.log(disableDates('2021-07-25')); console.log(disableDates('2021-07-02')); console.log(disableDates('2020-01-01'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

function f() {
    // some instructions

    for (let v of values) {
        if (v.from < v.to) {
            return true
        }
    }
    return false
}

 const values = [{ from: 2, to: 8 }, { from: 11, to: 20 }] var isOk = false; values.forEach(v => isOk = isOk || (v.from < v.to)) console.log(isOk)

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

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