2

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

  • 1
    To clarify, you will have an an array of objects like `obj1` and `obj2`, and you want to merge all of the objects in the array such that any `options` array that contains `null` will be overridden by the next object in the array? – lukewestby Dec 31 '14 at 01:35
  • Exactly but options array that contains null will be overridden not necessarily by the next object in the array, in fact by the object which has values for that field (options) –  Dec 31 '14 at 01:36
  • So the options array should be overridden by the first object to the right that does not contain `null` – lukewestby Dec 31 '14 at 01:41
  • Maybe my question was not clear; I now changed the names of objects now! they are not in order! –  Dec 31 '14 at 01:43
  • Okay, but be careful because order does matter in this problem. Starting at the beginning of the array of objects, do you want to keep the first `options` array which does not contains a non-null value? – lukewestby Dec 31 '14 at 01:47
  • In my case there is just one object in my array which as value for options! –  Dec 31 '14 at 01:48
  • Gotcha! Excellent. Last question - what about the `name` property, should this be taken from the first object in the array or will it follow the same pattern as `options`? – lukewestby Dec 31 '14 at 01:50
  • If an object has the value for a field ("name") we use it, if more than one object has values for a field (here "name") the values would be the same the order is not a big deal here; only we need to check if the value of one of fields is null (this can be array or even a simple field) we need to find the object which as the value for that field! –  Dec 31 '14 at 01:54
  • **http://jsfiddle.net/72eb4ry6/** – adeneo Dec 31 '14 at 01:56
  • @adeneo thanks man! but it seems the order has effect on your algorithm and the kast one always overwrite the previous ones! This has the same problem as my usage for lodash! –  Dec 31 '14 at 01:59
  • That's generally how extending objects work ? – adeneo Dec 31 '14 at 02:01

1 Answers1

0

Based on what you've explained in the question and comments, what we are trying to accomplish is selection of the object in myArray where the options does not contain null. In this case, we do not need to perform a merge, we can just search the array for the desired object.

var newResults = _.find(myArray, function (obj) {
    return !_.contains(obj.options, null);
});

newResults will be the first object in the array where options does not contain null.

lukewestby
  • 1,207
  • 8
  • 15