mergeDeep

node v5.12.0
version: 1.0.0
endpointsharetweet
// (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 _ in from) { if (_ in to) { _merge(to[_], from[_]) } else { to[_] = from[_] } } } sources.forEach(from => { _merge(to, from) }) return to } 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" } } } } } const assert = require('assert') assert.deepEqual(mergeDeep(us1, us2), { country: { "United States": { "Ford": { "engine": { type1: "4 cyl", type2: "6 cyl", type3: "12 cyl" } }, "Chevy": { "engine": { type1: "6 cyl" } }, "Saturn": { "engine": { type1: "4 cyl" } } } } })
Loading…

no comments

    sign in to comment