[英]how to modify only some object properties dinamically
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. 我需要从#2点更改对象属性的值为0的值。因此,在我的情况下,我想将rebateNetSaleMaxAmt2和rebateAmtAllProd2更改为其他值。 But I don't want to check or change properties from #1
但是我不想从#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 但我检查了所有属性,但不想查看/更改/检查#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.
我需要从#2点更改对象属性的值为0的值。因此,在我的情况下,我想将rebateNetSaleMaxAmt2和rebateAmtAllProd2更改为其他值。
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
数组
requiredKeys.forEach( function( key ){
console.log( key, dbValuesObj[key] )
})
For data you have provided, it will print 对于您提供的数据,它将打印
"rebateNetSaleMaxAmt2" 0
“ rebateNetSaleMaxAmt2” 0
"rebateAmtAllProd2" 0
“ rebateAmtAllProd2” 0
You need to check whether the property
ends with "2" and whether the property's value
is 0. 您需要检查
property
是否以“ 2”结尾以及属性的value
是否为0。
To get the last value from the property
string you can do the following: 要从
property
字符串中获取最后一个值,您可以执行以下操作:
property[property.length-1];
Then to get the value
of the property you can do this: 然后,要获取属性的
value
,可以执行以下操作:
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. 现在,您知道如何获取必需的组件,可以在for循环中使用if语句检查该属性是否满足您的要求(以“ 2”结尾且值为0),然后将该值更改为您所需要的值希望如此。 (in the code snippet I am setting it to 1)
(在代码段中,我将其设置为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. 您可以通过将groupNumber和defaultValue设置为该组中的
0
来尝试此操作。
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] = "";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.