Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if at least one condition is true in react js

I have an array of object tableRows.

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
    }
]

How do I check if at least one of the 9 key pairs value is greater than or equal to 100000 for each index.

Below Code didn’t work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!!!")
            }
}

Is there a better and more concise way to accomplish this?

>Solution :

If you already know that all entries of tableRows[index] are numbers you want to check, you can do this with Object.prototype.values and Array.prototype.some (which also short-circuits)

const 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},{purchase_sales:0,yearFirstRemAmount:0,capitalServicePurchase:0,otherServicePurchase:0,otherStuffPurchase:0,capitaStuffPurchase:0,currentYearServiceSell:0,currentYearStuffSell:0,yearLastRemAmount:0}];

const handleValidation = (index) => {
  if (!Object.values(tableRows[index]).some((v) => v >= 100000)) {
    alert("'At least one amount should be greater than or equal to 100000!!! for index " + index)
  }
}

tableRows.forEach((_row, idx) => handleValidation(idx));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading