我有一个多维对象数组,需要通过与对象数组进行比较来更新它。我展开了多维数组,并执行了嵌套的for each循环。我得到了重复的结果,我不知道我做错了什么。
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>
发布于 2017-07-13 20:16:16
您可以使用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>
发布于 2017-07-13 20:23:26
我认为这是一个逻辑错误,您正在将两个数组的元素加载到您的结果数组中。如果ID匹配,则跳过if (item.uid === sec.uid) result.push(秒)推流。
https://stackoverflow.com/questions/45080183
复制相似问题