检查react js中是否至少有一个条件为true



我有一个对象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
}
]

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

以下代码不起作用:

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

有没有更好、更简洁的方法来实现这一点?

如其他答案中所述,您可以将对象转换为数组或将其值转换为数组,然后在数组上使用some方法。

但是,如果你的对象可能有其他密钥,并且你想检查特定的属性:

  1. 您可以创建一个要检查的键数组:
const keys = [
'purchase_sales',
'yearFirstRemAmount',
'capitalServicePurchase',
'otherServicePurchase',
'otherStuffPurchase',
'capitaStuffPurchase',
'currentYearServiceSell',
'currentYearStuffSell',
'yearLastRemAmount'
];
  1. 创建一个函数,该函数接收对象作为输入并验证
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. 验证您的数据。类似于以下内容:
const isValid = tableRows.map(validate).every(Boolean)

您可以使用mapsome:的组合

const threshold = 1000000;
const yourAnswer = tableRows.map(Object.values).some(value => value >= threshold);
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!!!")
}
}

您可以使用filter方法过滤数组,Object.values键每个对象的值,some检查是否有等于或大于100000的键

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

const rows = tableRows.filter(row => {
return Object.values(row).some(value => value >= 100000)
})

console.log(rows)

如果您已经知道tableRows[index]的所有条目都是要检查的数字,则可以使用Object.prototype.valuesArray.prototype.some(它们也会短路(进行检查

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));

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
}
];
const handleValidation = (index) => {
const found = Object.values(tableRows[index]).some((x) => x > 100000);
if(!found)
{
alert("'At least one amount should be greater than or equal to 100000!!!");
}
console.log(index,found)
}
tableRows.forEach((item, index) => handleValidation(index));
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
}
];
let data = tableRows.filter((row)=>{
return Object.values(row).map(value => value >= 100000)
});
console.log(data);

最新更新