比较2个对象数组并用id替换对象

时间:2017-07-13 12:08:49

标签: javascript arrays multidimensional-array lodash

我有一个多维对象数组,我需要通过与对象数组进行比较来更新。我展平了多维数组并为每个循环做了嵌套。我得到了重复的结果,我无法弄清楚我做错了什么。

const newSections = [[{"id":9141118,"name":"cxz","qtr":"12/2016","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"593f35e7-c5b4-4c79-b4b8-0cc34dfd76a4"},{"id":9143204,"name":"cxz","qtr":"03/2017","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"9785f527-0c1f-414f-bd6b-9416da90a24f"}],[{"id":9141118,"name":" xzcxz","qtr":"12/2016","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"b6a78cf9-0de1-4465-bf7b-02b221330fcb"},{"id":9143204,"name":" xzcxz","qtr":"03/2017","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"9ef10b9b-b143-48a2-8e4f-fc2fdad4788b"}]]

const section = [{"id":9141118,"name":"cxz","qtr":"12/2016","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":"32","fut":0,"con":0,"tot":32,"storeTot":1,"variance":31,"uid":"593f35e7-c5b4-4c79-b4b8-0cc34dfd76a4"},{"id":9143204,"name":"cxz","qtr":"03/2017","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"9785f527-0c1f-414f-bd6b-9416da90a24f"}]

  let myNewArray = [].concat.apply([], newSections)
    let result = []
    _.each(myNewArray, item => {
      _.each(section, sec => {
        if (item.uid === sec.uid) result.push(sec)
        else result.push(item)
      })
    })

    console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

2 个答案:

答案 0 :(得分:1)

您可以使用find来避免嵌套循环(并解决您的复制错误):

const newSections = [[{"id":9141118,"name":"cxz","qtr":"12/2016","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"593f35e7-c5b4-4c79-b4b8-0cc34dfd76a4"},{"id":9143204,"name":"cxz","qtr":"03/2017","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"9785f527-0c1f-414f-bd6b-9416da90a24f"}],[{"id":9141118,"name":" xzcxz","qtr":"12/2016","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"b6a78cf9-0de1-4465-bf7b-02b221330fcb"},{"id":9143204,"name":" xzcxz","qtr":"03/2017","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"9ef10b9b-b143-48a2-8e4f-fc2fdad4788b"}]]

const section = [{"id":9141118,"name":"cxz","qtr":"12/2016","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":"32","fut":0,"con":0,"tot":32,"storeTot":1,"variance":31,"uid":"593f35e7-c5b4-4c79-b4b8-0cc34dfd76a4"},{"id":9143204,"name":"cxz","qtr":"03/2017","min":0,"max":0,"occ":0,"mod":0,"fv":0,"uc":0,"vdl":0,"fut":0,"con":0,"tot":0,"storeTot":1,"variance":-1,"uid":"9785f527-0c1f-414f-bd6b-9416da90a24f"}]

let myNewArray = [].concat.apply([], newSections)
let result = []
_.each(myNewArray, item => {
  let newItem = section.find((sec) => item.uid === sec.uid)
  
  result.push(newItem || item)
});

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

答案 1 :(得分:0)

我认为存在逻辑错误,您将两个数组的元素加载到结果数组中。如果ID匹配,你应该跳过if(item.uid === sec.uid)result.push(sec)。