javascriptrecursiones5-shim

Recursion between different objects and combine them uniquely, without duplicate


I've been trying to figure out how to do recursion for 2 objects that have similar properties, but also have differences. I need to merge these 2 objects in a unique way, so there are no duplicate countries or models etc.

EDIT: in vanilla js only please

var us1 = {
  country: {
    "United States": {
      "Ford": {
        "engine": {
          type1: "4 cyl",
          type2: "6 cyl"
        }
      },
      "Chevy": {
        "engine": {
          type1: "6 cyl"
        }
      }
    }
  }
}

var us2 = {
  country: {
    "United States": {
      "Ford": {
        "engine": {
          type3: "12 cyl"
        }
      },
      "Saturn": {
        "engine": {
          type1: "4 cyl"
        }
      }
    }
  }
}

var cars = [us1, us2];
var newCars = [];

function fn(cars) {
  if (typeof cars == "object") {
    for (var attr in cars) {
      if (!newCars.hasOwnProperty(cars[attr])) {
        newCars.push(cars[attr]);
      }

      fn(cars[attr])
    }
  } else {
    //
  }
}

console.log(fn(cars));
console.log(newCars)

Result wanted: var us1 = { country: { "United States": { "Ford": { "engine": { type1: "4 cyl", type2: "6 cyl", type2: "12 cyl" } }, "Chevy": { "engine": { type1: "6 cyl" } }, "Saturn": { "engine": { type1: "4 cyl" } } } } }


Solution

  • If you don't want to use a library, it's trivial to write yourself. Something along the lines of

    // (to: Object, ...sources: Object[]) => Object
    function mergeDeep(to) {
      const sources = Array.from(arguments).slice(1)
    
      // (to: Object, from: Object) => void
      const _merge = (to, from) => {
        for (let a in from) {
          if (a in to) {
            _merge(to[a], from[a])
          } else {
            to[a] = from[a]
          }
        }
      }
    
      sources.forEach(from => {
        _merge(to, from)
      })
    
      return to
    }
    

    See demo here https://tonicdev.com/bcherny/mergedeep

    But really, you should be using a library for this. Writing it yourself is guaranteed to be buggier and slower than any widely used existing implementation.