简体   繁体   English

使用 Lodash 进行深度合并

[英]Deep Merge using Lodash

I have two arrays of objects that contain addresses that have a label and an object for the actual address:我有两个 arrays 对象,它们包含的地址分别为 label 和 object 作为实际地址:

var originalAddresses = [
  {
    label: 'home',
    address: { city: 'London', zipCode: '12345' }
  },
  {
    label: 'work',
    address: { city: 'New York', zipCode: '54321' }
  }
];

var updatedAddresses = [
  {
    label: 'home',
    address: { city: 'London (Central)', country: 'UK' }
  },
  {
    label: 'spain',
    address: { city: 'Madrid', zipCode: '55555' }
  }
];

Now I want to merge these arrays by label and compare the individual properties of the addresses and merge only the properties from the new address that are actually present.现在我想通过label合并这些 arrays 并比较地址的各个属性并仅合并实际存在的新地址的属性。 So the result should look like this:所以结果应该是这样的:

var result = [
  {
    label: 'home',
    address: { city: 'London (Central)', zipCode: '12345', country: 'UK' }
  },
  {
    label: 'work',
    address: { city: 'New York', zipCode: '54321' }
  },
  {
    label: 'spain',
    address: { city: 'Madrid', zipCode: '55555' }
  }
]

How can I do this using lodash ?我怎样才能使用lodash做到这一点? I tried a combination of unionBy() and merge() .我尝试了unionBy()merge()的组合。 With unionBy() I was able to compare and join the arrays by label, but this always replaces the whole object. I can sure merge the addresses but this doesn't happen by label then.使用 unionBy(),我能够通过 label 比较并加入 arrays,但这总是会替换整个 object。我可以确定合并地址,但到 label 时不会发生这种情况。

You can turn both arrays into objects using _.keyBy(arr, 'label') , and then merge deep using _.merge() : 您可以使用_.keyBy(arr, 'label')将两个数组转换为对象,然后使用_.merge()深度合并:

 var originalAddresses = [{ label: 'home', address: { city: 'London', zipCode: '12345' } }, { label: 'work', address: { city: 'New York', zipCode: '54321' } }]; var updatedAddresses = [{ label: 'home', address: { city: 'London (Central)', country: 'UK' } }, { label: 'spain', address: { city: 'Madrid', zipCode: '55555' } }]; var result = _.values(_.merge( _.keyBy(originalAddresses, 'label'), _.keyBy(updatedAddresses, 'label') )); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script> 

I try to use VanillaJS to handle this.我尝试使用 VanillaJS 来处理这个问题。

const originalAddresses = [{
    label: 'home',
    address: {
        city: 'London',
        zipCode: '12345'
    }
}, {
    label: 'work',
    address: {
        city: 'New York',
        zipCode: '54321'
    }
}];

const updatedAddresses = [{
    label: 'home',
    address: {
        city: 'London (Central)',
        country: 'UK'
    }
}, {
    label: 'spain',
    address: {
        city: 'Madrid',
        zipCode: '55555'
    }
}];


const groupBy = (array, property) => {
    return array.reduce((acc, cur) => {
            let key = cur[property]
            if (!acc[key]) {
                acc[key] = []
            }
            acc[key].push(cur)

            return acc
        }
        , {})
}

const groupByLabel = groupBy([...originalAddresses, ...updatedAddresses], 'label')

const result  = Object.keys(groupByLabel).map((key) => {
        return {
            label: groupByLabel[key][0].label,
            address: groupByLabel[key].reduce((acc, cur) => {
                    return Object.assign(acc, cur.address)
                }
                , {})
        }
    }
)
console.log(result)

You can turn both arrays into objects using _.keyBy(arr, 'label'), and then merge deep using _.merge():您可以使用 _.keyBy(arr, 'label') 将两个数组转换为对象,然后使用 _.merge() 进行深度合并:

var originalAddresses = [{
  label: 'home',
  address: {
    city: 'London',
    zipCode: '12345'
  }
}, {
  label: 'work',
  address: {
    city: 'New York',
    zipCode: '54321'
  }
}];

var updatedAddresses = [{
  label: 'home',
  address: {
    city: 'London (Central)',
    country: 'UK'
  }
}, {
  label: 'spain',
  address: {
    city: 'Madrid',
    zipCode: '55555'
  }
}];

var result = _.values(_.merge(
  _.keyBy(originalAddresses, 'label'),
  _.keyBy(updatedAddresses, 'label')
));

console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM