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:
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?
CodePudding user response:
As mentioned in other answers, you can convert an object to an array or convert its values to an array and then use some
method` on array.
But if your object may have other keys and you want to check specific properties:
- You can create an array of keys that you want to check:
const keys = [
'purchase_sales',
'yearFirstRemAmount',
'capitalServicePurchase',
'otherServicePurchase',
'otherStuffPurchase',
'capitaStuffPurchase',
'currentYearServiceSell',
'currentYearStuffSell',
'yearLastRemAmount'
];
- Create a function which receives an object as an input and validates
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;
}
- Validate your data. Something like this:
const isValid = tableRows.map(validate).every(Boolean)
CodePudding user response:
You can use a combination of map
and some
:
const threshold = 1000000;
const yourAnswer = tableRows.map(Object.values).some(value => value >= threshold);
CodePudding user response:
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!!!")
}
}
CodePudding user response:
You can use filter
method to filter the array, and Object.values
keys value of each object, and some
to check if there a key equal or more than 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)
CodePudding user response:
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));
CodePudding user response:
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));
CodePudding user response:
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);