30

Two part question about similar problems. I've got 2 different arrays,

array 1:

 array1 = [{
     name : "users",
     checked : true
   }, {
     name : "active users",
     checked : false
   }, {
     name : "completions",
     checked : false
   }]

I would like to know if there is an easy way to set all the checked values to true at once. I know this is possible with a for loop:

  for (var i = 0 ; i < array.length ; i++) {
    array[i].checked = false
  }

Even though this is not a lot of code i'm just curious if there is a way to do this without iterating over every object in the array. (not sure if this is even possible or whether this makes any difference whatsoever, so if please correct me if i am wrong about any of this).

array 2:

array2 = [{
     value1 : false,
     value2 : true,
     value3 : false,
     value4 : false, 
     value5 : false,   
   }]

Is it possible to simply set all the values to true or false at once (again i can use a for loop, but would prefer not to)

(i'm using angular and lodash, but haven't been able to find a 'lodash' or 'angular' solution, but any solution will do)

Mischa
  • 2,069
  • 4
  • 23
  • 32
  • array2 only has 1 element: an object with five properties. If it were an array of values or an array of properties, and if using ES6 is okay, then you could use [Array.prototype.fill()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill) – Kenigmatic Dec 06 '17 at 20:32

4 Answers4

38

You can't do this without looping over the elements, however there are functional abstractions you can use instead of for:

For array 1, you can use map

array1.map(function(x) { 
  x.checked = true; 
  return x
});

Or using _.map from lodash:

_.map(array1, function(x) { 
  x.checked = true; 
  return x
});

For array 2 you can use _.mapValues from lodash.

_.mapValues(array2[0], function() {
  return true;
});

You can see that lodash implements _.map using while at https://github.com/lodash/lodash/blob/a1b15df6489f47498edda244552c9804e046a45d/lodash.js#L3125

Gazler
  • 83,029
  • 18
  • 279
  • 245
25

You can use es6 fat arrow and make one liner but still it involves iteration .

array1.map(a=>a.checked=true);

array1 = [{
     name : "users",
     checked : true
   }, {
     name : "active users",
     checked : false
   }, {
     name : "completions",
     checked : false
   }]
   array1.map(a=>a.checked=true);
   console.log(array1);
sumit
  • 15,003
  • 12
  • 69
  • 110
  • Using `.map()` without using its return value [is an anti-pattern](https://thenewtoys.dev/blog/2021/04/17/misusing-map/). Simply use [`.forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) in this case. – Ivar Jan 17 '23 at 15:04
10

Even though this is not a lot of code i'm just curious if there is a way to do this without iterating over every object in the array. (not sure if this is even possible or whether this makes any difference whatsoever, so if please correct me if i am wrong about any of this).

Is it possible to simply set all the values to true or false at once (again i can use a for loop, but would prefer not to)

No ! You can't visit all towns without visiting each of them. There are shortcuts with jQuery like $.each and lodash like _.map but it's just a function that's doing what you would do with the for loop.

Fourat
  • 2,366
  • 4
  • 38
  • 53
1

There is no way to set the value of each object in an array without iterating (looping) through the array as you mentioned in your question.

eskimomatt
  • 195
  • 9