I have a few objects with the same structure; some of them have null values for some fields, whereas one of the objects (here obj2) has values for the field which has null values in other objects; I want ro merge them and cosider the one which has value:
var obj2 = {
options: [
null
],
name: "name1",
}
var obj1 = {
options: [
{"option": "option1"}
],
name: "name1",
}
desiredOutput = {
options: [
{"option": "option1"}
],
name: "name1",
}
My attempt: I tried lodash; but the prpoblem with lodash is that the order has effects; that is, whichever object comes last that one overwrites the previous ones for the mentioned field:
I tried lodash; here myArray is an array of obj1, obj2,...
var newResults = {};
for (var i = 0; i < myArray.length; i++) {
newResults = lodash.extend(newResults, myArray[i]);
}
What is the cleanest solution for this one? Thanks