tharindu
tharindu

Reputation: 523

Replacing a value of a JavaScript array object using js

I have a JavaScript array as follows

var array = [{"month": 1, "days": 31},{"month": 2, "days": 2},{"month": 3, "days": 21}]

I need to replace days value of month 1 to 12 as follows

var array = [{"month": 1, "days": 12},{"month": 2, "days": 2},{"month": 3, "days": 21}]

How can I get this. I tried the following way. But it's not working

var val = array.indexOf(1);
for(var i=0; i <array .length; i++ )
{
if(val != -1)
array.month[val].push({month:1, days:12 }); 
}

Upvotes: 0

Views: 85

Answers (5)

D. Geren
D. Geren

Reputation: 101

First, it may just be an example, but avoid using array as it is too close to Array, an object of JavaScript. I will use arr for array.

Just to change the value of days in month 1 to 12:

arr[0].days = 12;

However, if you need the ability to change any given month's day value, try a function:

  function changeDays(month, days) {
    for(var i = 0; i < arr.length; ++i){
      if(arr[i].month == month) arr[i].days = days;
    };
  };

  changeDays(1, 12); // changes 31 days to 12 days in month 1

The other answers from Nick and TrickOrTreat are more elegant, and Parth's answer assumes you need to search for a days value instead of month value. My solution is closest to your example and it makes more sense to me that you would want to search by month. But hey, you know better than any of us what your requirements are!

Upvotes: 0

Azad
Azad

Reputation: 5264

you can use forEach function to accomplish this,

var array = [{"month": 1, "days": 31},{"month": 2, "days": 2},{"month": 3, "days": 21}]

array.forEach(e=>{

  if(e.month == 1)
    e.days = 12;
});

console.log(array);

Upvotes: 0

TrickOrTreat
TrickOrTreat

Reputation: 911

You can use 'of' keyword also to achieve this, however what you want to change totally depends on u, here is a little example:

var array = [{"month": 1, "days": 31},{"month": 2, "days": 2},{"month": 3, "days": 21}];
for(var i of array){if(i.month === 1){i.days = 12}}
console.log(array)

Upvotes: 1

Parth Raval
Parth Raval

Reputation: 4413

var array = [{
  "month": 1,
  "days": 31
}, {
  "month": 2,
  "days": 2
}, {
  "month": 3,
  "days": 21
}];
for (var i = 0; i < array.length; i++) {
  if (array[i].days === 31) {
    array[i].days = 12
  }
}
console.log(array)

You can replace the particular element by identifying it.

Upvotes: -1

Nick Parsons
Nick Parsons

Reputation: 50664

You can .map() your array and change the mapped object when month: 1 appears like so:

var array = [{"month": 1, "days": 31},{"month": 2, "days": 2},{"month": 3, "days": 21}];

var new_array = array.map(({month, days}) => month === 1 ? {month, days: 12} : {month, days});
console.log(new_array);

Alternatively, if you wish to change your array in place, you can use .filter() and then change the days attribute on the filtered objects like so:

var array = [{"month": 1, "days": 31},{"month": 2, "days": 2},{"month": 3, "days": 21}];

array.filter(({month}) => month === 1).forEach(o => o.days = 12);
console.log(array);

Upvotes: 4

Related Questions