Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript set all values in array of object

Tags:

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)

like image 274
Mischa Avatar asked May 07 '15 10:05

Mischa


People also ask

How do you set the value of an array of objects?

To change the value of an object in an array: Call the findIndex() method to get the index of the specific object. Access the array at the index and change the property's value using dot notation. The value of the object in the array will get updated in place.

How do you manipulate an array of objects in JavaScript?

Our function should search the first array for objects with the same "id" property that is present in the second array. Then our function should replace the "name" property of those objects with the corresponding "name" property of the second array's objects.

How do you change an object in an array of objects?

To update an object's property in an array of objects, use the map() method to iterate over the array. On each iteration, check if the current object is the one to be updated. If it is, modify the object and return the result, otherwise return the object as is. Copied!

How do you change the value in an array of objects react?

To update an array of objects state in React: Use the map() method to iterate over the array. On each iteration, check if a certain condition is met. Update the properties of the object that matches the condition.


1 Answers

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

like image 99
Gazler Avatar answered Sep 21 '22 04:09

Gazler