25

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

3 Answers 3

29

You don't need lodash for this.


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


Showing two ways:


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

Sign up to request clarification or add additional context in comments.

1 Comment

Immutable ES6/ES2015 version: const newArr = arr.map(e => ({...e, status: "active"}))
14

Indeed, you don't need Lodash, but the question is tagged Lodash, and using Lodash offers some useful defenses that reduces the risk of errors. This solution utilizes _.forEach and _.set

 // _.forEach won't throw errors if arr is not an array...
 _.forEach(arr, function (obj) {
    // _.set won't throw errors if obj is not an object. With more complex objects, if a portion of the path doesn't exist, _.set creates it
     _.set(obj, 'status', 'active');
 });

If you wanted to make it abstract, you could build a Lodash mixin:

_.mixin({
    setProperty: function(arr, key, val) {
        _.forEach(arr, function (obj) {
            _.set(obj, path, val);
        });
    }
});

Then, you could use it exactly as you described:

_.setProperty( arr, 'status', 'active' );

3 Comments

Nice 'mixin' example! Another possibility, though a bit of a 'loaded' one-liner: _.map(arr, function(o) { _.set(o, 'status', 'active'); });.
Another one-liner: _.each(arr, itm => _.set(itm, 'status', 'active'));
you don't need lodash to do simple stuff like this.
3

A way simpler and and cleaner way !

If you want to use func programming in a proper manner

  myArray = myArray.map(arrayElem => {
    arrayElem.property = newValue
    return arrayElem
  })

1 Comment

ESLint: Assignment to property of function parameter 'i'.(no-param-reassign)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.