Raja G
Raja G

Reputation: 6643

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.

Upvotes: 0

Views: 80

Answers (6)

FZs
FZs

Reputation: 18609

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

Upvotes: 2

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

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

Upvotes: 0

Stratubas
Stratubas

Reputation: 3067

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

Upvotes: 1

ellipsis
ellipsis

Reputation: 12152

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

Upvotes: 1

MBadrian
MBadrian

Reputation: 409

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

Upvotes: 1

Mohamed Farouk
Mohamed Farouk

Reputation: 1107

Try the below code:

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

Upvotes: 1

Related Questions