首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检查在react js中是否至少有一个条件为真

检查在react js中是否至少有一个条件为真
EN

Stack Overflow用户
提问于 2022-08-07 07:23:30
回答 7查看 240关注 0票数 -1

我有一个对象tableRows数组。

代码语言:javascript
运行
复制
tableRows = [
    {
        purchase_sales:1000,
        yearFirstRemAmount: 1456,
        capitalServicePurchase:123234,
        otherServicePurchase: 12323,
        otherStuffPurchase: 8903,
        capitaStuffPurchase: 1200,
        currentYearServiceSell: 47856,
        currentYearStuffSell: 100000,
        yearLastRemAmount: 20000
    }
    {
        purchase_sales:23430,
        yearFirstRemAmount: 12500,
        capitalServicePurchase: 1000010,
        otherServicePurchase: 12360,
        otherStuffPurchase: 12300,
        capitaStuffPurchase: 12000,
        currentYearServiceSell: 123123,
        currentYearStuffSell: 12111,
        yearLastRemAmount: 13120
    }
]

如何检查每个索引的9个键对值中是否至少有一个大于或等于100000。

以下代码不起作用:

代码语言:javascript
运行
复制
const handleValidation = (index)=>{
    if(tableRows[index].purchase_sales<100000 || tableRows[index].yearFirstRemAmount<100000 || tableRows[index].capitalServicePurchase<100000 || tableRows[index].otherServicePurchase<100000 || tableRows[index].otherStuffPurchase<100000 || tableRows[index].capitaStuffPurchase<100000 || tableRows[index].currentYearServiceSell<100000 || tableRows[index].currentYearStuffSell<100000 || tableRows[index].yearLastRemAmount<100000 ){
                alert("'At least one amount should be greater than or equal to 100000!!!")
            }
}

是否有更好、更简洁的方法来完成这一任务?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2022-08-07 07:44:39

正如在其他答案中提到的,您可以将对象转换为数组或将其值转换为数组,然后使用some method`‘on数组。

但是,如果您的对象可能有其他键,并且希望检查特定的属性:

  1. 您可以创建一个要检查的键数组:

代码语言:javascript
运行
复制
const keys = [
    'purchase_sales',
    'yearFirstRemAmount',
    'capitalServicePurchase',
    'otherServicePurchase',
    'otherStuffPurchase',
    'capitaStuffPurchase',
    'currentYearServiceSell',
    'currentYearStuffSell',
    'yearLastRemAmount'
];

  1. 创建一个函数,该函数接收作为输入的对象并验证

代码语言:javascript
运行
复制
function validate (obj) {
    for (key of keys) {
        if(obj[key] >= 100000) {
            // Do something here.
            // For instance: return true;
        }
    }
    // There is no property with a value greater or equal to 100000.
    // Do something here.
    // For instance: return false;
}

  1. 验证您的数据。就像这样:

代码语言:javascript
运行
复制
    const isValid = tableRows.map(validate).every(Boolean)
票数 0
EN

Stack Overflow用户

发布于 2022-08-07 07:30:22

您可以使用mapsome的组合

代码语言:javascript
运行
复制
const threshold = 1000000;
const yourAnswer = tableRows.map(Object.values).some(value => value >= threshold);
票数 0
EN

Stack Overflow用户

发布于 2022-08-07 07:31:58

代码语言:javascript
运行
复制
const handleValidation = (index)=>{
 
 if (Object.values(tableRows[index]).some(value => value < 100000)) {
    alert("'At least one amount should be greater than or equal to 100000!!!")
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73265622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档