shopia T shopia T - 1 month ago 25
Javascript Question

Add new properties of object in lodash

I've two objects and I want to add properties from object A to object B and I try with extend which doesn't work,do I need to use something different ?

a = {
name = "value"
name2 = "value2"
}

b = {
name3 = "value"
name4 = "value2"
}


I want that A will contain both

a = {
name = "value"
name2 = "value2"
name3 = "value"
name4 = "value2"
}

Answer

_.extend (now called _.assign) is indeed how you do this:

_.assign(a, b);

Live Example:

var a = {
name: "value",
name2: "value2"
};

var b = {
name3: "value",
name4: "value2"
};

_.assign(a, b);
document.body.insertAdjacentHTML(
  "beforeend",
  "Result:<pre>" + JSON.stringify(a, null, 2) + "</pre>"
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>