我正在开发React Native应用 说我有这些数组列表:
let soldList1 = [
{"itemCode":"X001" , "soldRate":0.0789},
{"itemCode":"5555" , "soldRate":0.0543},
{"itemCode":"3141" , "soldRate":0.0112},
{"itemCode":"Mix-001" , "soldRate":0.01},
{"itemCode":"7689-L" , "soldRate":0.005},
{"itemCode":"1111" , "soldRate":0.003}
]
let soldList2 = [
{"itemCode":"3141" , "soldRate":0.0712},
{"itemCode":"7689-L" , "soldRate":0.03},
{"itemCode":"5555" , "soldRate":0.0234},
{"itemCode":"1111" , "soldRate":0.011},
{"itemCode":"X001" , "soldRate":0.008},
{"itemCode":"Mix-001" , "soldRate":0.004}
]
let soldList3 = [
{"itemCode":"5555" , "soldRate":0.0339},
{"itemCode":"X001" , "soldRate":0.0221},
{"itemCode":"3141" , "soldRate":0.0111},
{"itemCode":"1111" , "soldRate":0.0089},
{"itemCode":"Mix-001" , "soldRate":0.0077},
{"itemCode":"7689-L" , "soldRate":0.0032}
]
let soldList4 =[
{"itemCode":"8888" , "soldRate":0.13},
{"itemCode":"9999" , "soldRate":0.11},
{"itemCode":"3141" , "soldRate":0.08},
{"itemCode":"1111" , "soldRate":0.07}
]
let soldList5 =[
{"itemCode":"3141" , "soldRate":0.044},
{"itemCode":"1111" , "soldRate":0.011},
{"itemCode":"8888" , "soldRate":0.0011},
{"itemCode":"9999" , "soldRate":0.0001}
]
let soldList6 =[
{"itemCode":"Mix-001" , "soldRate":0.5678},
{"itemCode":"7689-L" , "soldRate":0.546}
{"itemCode":"8888" , "soldRate":0.323},
{"itemCode":"9999" , "soldRate":0.0032},
{"itemCode":"Mix-001" , "soldRate":0.0022},
{"itemCode":"UV-007" , "soldRate":0.0012}
{"itemCode":"TT-08" , "soldRate":0.0011},
{"itemCode":"PP-03" , "soldRate":0.0009}
]
如您所见,某些列表包含元素,而其他列表则没有,每个列表项的顺序为desc order by 'soldRate'
,并且每个list.length也可能不同。
所以目标是连接这些数组列表并进行处理以构建一个新的数组列表,如下所示:
let finalAllConcateAndSortedByDataSumList = [
{"itemCode":"Mix-001" , "data": [0.01, 0.004, 0.0077, 0, 0, 0.5678], "dataSum":0.5895},
{"itemCode":"7689-L" , "data": [0.005, 0.03, 0.0032, 0, 0, 0.546], "dataSum":0.5842},
{"itemCode":"8888" , "data": [0, 0, 0, 0.13, 0.0011, 0.323], "dataSum":0.4541},
{"itemCode":"3141" , "data": [0.0112, 0.0712, 0.0111, 0.08, 0.044, 0], "dataSum":0.2175},
{"itemCode":"X001" , "data": [0.0789, 0.008, 0.0221, 0, 0, 0.0221], "dataSum":0.1311},
{"itemCode":"9999" , "data": [0, 0, 0, 0.11, 0.0001, 0.0032], "dataSum":0.1133},
{"itemCode":"5555" , "data": [0.0543, 0.0234, 0.0339, 0, 0, 0], "dataSum":0.1116},
{"itemCode":"1111" , "data": [0.003, 0.011, 0.0089, 0.07, 0.011, 0], "dataSum":0.1039},
{"itemCode":"UV-007" , "data": [0, 0, 0, 0, 0, 0.0012], "dataSum":0.0012},
{"itemCode":"TT-08" , "data": [0, 0, 0, 0, 0, 0.0011], "dataSum":0.0011},
{"itemCode":"PP-03" , "data": [0, 0, 0, 0, 0, 0.0009], "dataSum":0.0009},
]
因此,如您所见,上面列表1 2 3 4 5 6中的所有'soldRate'
元素都已连接并生成为新的数组列表'data'
属性,每个数据顺序的位置都非常醒目与上面的原始列表相同1 2 3 4 5 6 ....如果在任何原始列表中不存在该位置的元素值,则其为'0'值
最后是finalAllConcateAndSortedByDataSumList中的每个对象,数据数组将汇总为属性'dataSum'
然后新列表finalAllConcateAndSortedByDataSumList为desc order by dataSum
这是我个人尝试使其起作用,但是不起作用...
let finalAllConcateAndSortedByDataSumList = soldList1.concat(soldList2)
.concat(soldList3)
.concat(soldList4)
.concat(soldList5)
.concat(soldList16).map((item) => {
let newItem = { itemCode: item.itemCode, data: [item.soldRate], dataSum: Math.sum([item.soldRate])}
return newItem
}).sort((item) => { return item.dataSum})
因此在这里寻求帮助,代码示例将非常有帮助 谢谢
答案 0 :(得分:1)
您可以首先提取数组中存在的所有唯一的itemCode
值,然后在它们上循环,然后将soldRate
或0
添加到结果中,如果{{1 }}存在于每个数组中。
itemCode
答案 1 :(得分:1)
我将您的过程分为多个步骤,因为它可以使您更清楚地知道自己在做什么(并且更容易调试)。该代码可能不像其他答案那么紧凑或“优雅”,但是我相信这样更容易理解。
这里是一个code sandbox,显示了它的运行情况(请参阅控制台输出)。
return_address
答案 2 :(得分:0)
我会在字典中使用数组的组合:
let result = {}
soldList1.concat(soldList2)
.concat(soldList3)
.concat(soldList4)
.concat(soldList5)
.concat(soldList6)
.forEach(item => {
if(result[item.itemCode]) {
result[item.itemCode].data.push(item.soldRate);
result[item.itemCode].dataSum += item.soldRate;
} else {
result[item.itemCode] = {data: [item.soldRate], itemCode: item.itemCode, dataSum: item.soldRate};
}
});
let finalAllConcateAndSortedByDataSumList = Object.values(result).sort((item) => { return item.dataSum})
console.log(finalAllConcateAndSortedByDataSumList);