I have this object (that has a lot more properties than listed here, having different values):
var dbValuesObj = {
//1. group of properties that I don't want to touch(I don't want to check the values for these)
rebateNetSaleMinAmt1: 500,
rebateNetSaleMaxAmt1: 400,
rebateAmtWoProd1: 0,
rebateAmtAllProd1: 200,
rebateAmtOneProd1: 0,
//2. group of properties that I want to change (I know exactly what is the list of the properties that I want to check and change their values, if they are equal to 0)
rebateNetSaleMinAmt2: 100,
rebateNetSaleMaxAmt2: 0,
rebateAmtWoProd2: 300,
rebateAmtAllProd2: 0,
rebateAmtOneProd2: 700
}
I need to change the values for the object properties from point #2 that have value 0. So in my case, I want to change rebateNetSaleMaxAmt2 and rebateAmtAllProd2 to a different value. But I don't want to check or change properties from #1
I have tried using
for(var property in dbValuesObj)
but I checks all the properties and I don't want to look/change/check the properties from #1
I need to change the values for the object properties from point #2 that have value 0. So in my case, I want to change rebateNetSaleMaxAmt2 and rebateAmtAllProd2 to a different value.
You can filter them out first
var requiredKeys = Object.keys( dbValuesObj ).filter( k => k.slice(-1) == "2" && dbValuesObj[k] == 0 );
Now you can iterate this requiredKeys
array
requiredKeys.forEach( function( key ){
console.log( key, dbValuesObj[key] )
})
For data you have provided, it will print
"rebateNetSaleMaxAmt2" 0
"rebateAmtAllProd2" 0
You need to check whether the property
ends with "2" and whether the property's value
is 0.
To get the last value from the property
string you can do the following:
property[property.length-1];
Then to get the value
of the property you can do this:
dbValuesObj[property];
Now that you know how to get required components you can use an if statement in you for loop to check whether or not the property meets your requirements (ends with "2" and has a value of 0) and then change the value to whatever you want it to be. (in the code snippet I am setting it to 1)
Check the code snippet for a working example:
var dbValuesObj = { rebateNetSaleMinAmt1: 500, rebateNetSaleMaxAmt1: 400, rebateAmtWoProd1: 0, rebateAmtAllProd1: 200, rebateAmtOneProd1: 0, rebateNetSaleMinAmt2: 100, rebateNetSaleMaxAmt2: 0, rebateAmtWoProd2: 300, rebateAmtAllProd2: 0, rebateAmtOneProd2: 700 } for (var property in dbValuesObj) { var value = dbValuesObj[property]; if (property[property.length - 1] === "2" && value === 0) { dbValuesObj[property] = 1; // Change the value to a new value (ie 1) } } console.log(dbValuesObj);
function updateGroup(obj, defaultValue, grpNum) {
for(prop in obj ) {
var index = parseInt(prop.match(/\d+$/)[0]);
if(index === grpNum && obj[prop] === 0) {
obj[prop] = defaultValue;
}
if(index > grpNum) { // if properties are in number order. else remove this.
return;
}}
return;
}
updateGroup(dbValuesObj, 100, 2);
You can try this by giving groupNumber and defaultValue you what to set for 0
in that group.
So this is how i resolved my issue Thanks everyone for the answers
//Define the properties that we want to check for 0 value
var arrayOfPropsToCheck = ["rebateNetSaleMinAmt1", "rebateNetSaleMaxAmt1", "rebateAmtWoProd1", "rebateAmtAllProd1", "rebateAmtOneProd1",
"rebateNetSaleMinAmt2", "rebateNetSaleMaxAmt2", "rebateAmtWoProd2", "rebateAmtAllProd2", "rebateAmtOneProd2",
"rebateNetSaleMinAmt3", "rebateNetSaleMaxAmt3", "rebateAmtWoProd3", "rebateAmtAllProd3", "rebateAmtOneProd3",
"rebateNetSaleMinAmt4", "rebateNetSaleMaxAmt4", "rebateAmtWoProd4", "rebateAmtAllProd4", "rebateAmtOneProd4",
"rebateNetSaleMinAmt5", "rebateNetSaleMaxAmt5", "rebateAmtWoProd5", "rebateAmtAllProd5", "rebateAmtOneProd5",
"rebateNetSaleMinAmt6", "rebateNetSaleMaxAmt6", "rebateAmtWoProd6", "rebateAmtAllProd6", "rebateAmtOneProd6",
"rebateNetSaleMinAmt7", "rebateNetSaleMaxAmt7", "rebateAmtWoProd7", "rebateAmtAllProd7", "rebateAmtOneProd7"];
//Check the properties in the dbValuesObj object
for (var property in dbValuesObj) {
//Get the value for the property
var value = dbValuesObj[property];
//Define flag
var isInArray = false;
//Get the values from the arrayOfPropsToCheck
for(var k = 0; k < arrayOfPropsToCheck.length; k++){
//Compare if the name of the property is equal to the one found in the array and set flag to true
if(arrayOfPropsToCheck[k] == property){
isInArray = true;
}
}
//If propery is in arrayOfPropsToCheck and has value 0 change it to empty string
if(isInArray && value === "0"){
dbValuesObj[property] = "";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.