简体   繁体   中英

JavaScript : How to Increment a object attribute property

I am trying to increment an attribute property of JavaScript object. My code looks like below

var objVal = { "date" : "2019-07-01", "count" : "0" };
var updateObj = { "date" : "", "count" : Number(count) + 1 };
for (var key in updateObj) {
  objVal[key] = updateObj[key];
}

console.log(JSON.stringify(objVal));

I want to empty the date and increase count attribute by 1 .

Any suggestions are greatly appreciated.

You could use a mapper function instead of values in your update object:

 var objVal = { "date": "2019-07-01", "count": "0" }; var updateObj = { "date": "", "count": count => Number(count) + 1 }; for (var key in updateObj) { if (typeof updateObj[key] === 'function') { objVal[key] = updateObj[key](objVal[key]); } else { objVal[key] = updateObj[key]; } } console.log(JSON.stringify(objVal));

Now if a property of the update object is a function, this will call it with the old value as parameter, and updates the object using its return.

If a non-function is present as a key of the update object, this code will behave like yours.

If you'd like to change a property of objVal into a function, you'll need to put a function-returning-function inside updateObj

Instead of count use count property of the initial object

 var objVal = { "date" : "2019-07-01", "count" : "0" }; var updateObj = { "date" : "", "count" : Number(objVal['count']) + 1 }; for (var key in updateObj) { objVal[key] = updateObj[key]; } console.log(JSON.stringify(objVal));

Try the below code:

 var objVal = { "date" : "2019-07-01", "count" : "0" }; objVal["count"] = (parseInt(objVal["count"])+1).toString() console.log(JSON.stringify(objVal));

you must be change your code like this

 var objVal = {"date" : "2019-07-01", "count" : "0" }; objVal.date=""; objVal.count=Number(objVal.count)+1 console.log(JSON.stringify(objVal));

I'm feeling you are looking for a more dynamic solution, like this:

 const objVal = {"date": "2019-07-01", "count": "0"}; const updateObj = { "date": "empty", "count": "increment", // maybe you'll come up with more action types in the future... }; for (const key in updateObj) { const action = updateObj[key]; if (action === 'empty') { objVal[key] = ''; continue; } if (action === 'increment') { objVal[key] = (Number(objVal[key]) + 1).toString(); continue; } // handle any other actions... } console.log(objVal);

It is similar to FZs's answer, but it allows you to store the updateObj more easily (in a database / a JSON file / whatever).

To make it extended support, you can use some thing like this.

 var objVal = { "date" : "2019-07-01", "count" : "0" }; var keysToEmpty = ["date"]; var keysToNumber = ["count"]; for (var key in objVal) { if (keysToEmpty.includes(key)) { objVal[key] = ""; } else if (keysToNumber.includes(key)) { objVal[key] = Number(objVal[key]) + 1; } } console.log(JSON.stringify(objVal));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM