Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set specific property value of all objects in a javascript object array (lodash)

I have following object array:

var arr = [
  {
    id    : "a1",
    guid  : "sdfsfd",
    ...
    value : "abc",
    status: "active"
  },
  {
    id    : "a2",
    guid  : "sdfsfd",
    ...
    value : "def",
    status: "inactive"
  },
  {
    id    : "a2",
    guid  : "sdfsfd",
    ...
    value : "def"
  },
  ...
]

How to set "status" property of each object to "active". So the resulting array will be:

var arr = [
  {
    id    : "a1",
    guid  : "sdfsfd",
    ...
    value : "abc",
    status: "active"
  },
  {
    id    : "a2",
    guid  : "sdfsfd",
    ...
    value : "def",
    status: "active"
  },
  {
    id    : "a2",
    guid  : "sdfsfd",
    ...
    value : "def",
    status: "active"
  },
  ...
]

Additionally this should create the property "active" if doesn't exists.

I can do this using for loops. But I'm pretty much sure lodash can do this in one line like:

arr = _.set_property(arr, "status", "active");
like image 508
sith Avatar asked Feb 16 '17 23:02

sith


People also ask

How do you assign a value to an object property?

Object.assign() Method Among the Object constructor methods, there is a method Object. assign() which is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target.

How do you change the properties of an object in array typescript?

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 I add a conditional property to an object?

To conditionally add a property to an object, we can make use of the && operator. In the example above, in the first property definition on obj , the first expression ( trueCondition ) is true/truthy, so the second expression is returned, and then spread into the object.

Can we use indexOf with object?

To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.


1 Answers

You don't need lodash for this.


The first object is missing your status property and it will be added.


SHOWING THREE WAYS HOW YOU CAN DO IT


IMMUTABLE VERSION (We create a new array using map)

const arrImmutableVersion = arr.map(e => ({...e, status: "active"}));

MUTABLE VERSIONS (We change the original array)

arr.forEach((el)=>{el.status = "active";}) 

or

arr.forEach(function(el){el.status = "active";}) 

var arr = [
  {
    id    : "a1",
    guid  : "sdfsfd",   
    value : "abc"
  },
  {
    id    : "a2",
    guid  : "sdfsfd",   
    value : "def",
    status: "inactive"
  },
  {
    id    : "a2",
    guid  : "sdfsfd",   
    value : "def",
    status: "active"
  } 
];

// SHOWING THREE WAYS HOW YOU CAN DO IT

// MUTABLE VERSIONS - We change the original array
arr.forEach((el)=>{el.status = "active";}) // ES6
// or
arr.forEach(function(el){el.status = "active";}) 
//or
// IMMUTABLE VERSION - We create a new array using `map`
const arrImmutableVersion = arr.map(e => ({...e, status: "active"})); // ES6
//--------------------------------------------------------------


// RESULTS:
console.log("logging results of object 'arr'");
console.log(arr);
console.log("---------------------------------------------------------");
console.log("logging results of object 'arrImmutableVersion'");
console.log(arrImmutableVersion);
like image 136
Legends Avatar answered Sep 30 '22 09:09

Legends