我的输入列表:
任务是循环浏览下面给出的输入列表,并找到最终输出,该输出将列表"值"与以下合并策略合并。
- 主键是以下键
code+brc+dest
的组合。如果列表的其他索引中的任何值与该键匹配,我们需要检查类型并返回最严格的类型(在我的情况下为"HARD")
例如:
index 0 value : `[{ "code": 1,"brc":"ABC","dest":"des1","type":"SOFT"}]`
index 1 value : `[{ "code": 1,"brc":"ABC","dest":"des1","type":"HARD"}],`
index 2 value : `[{ "code": 1,"brc":"ABC","dest":"des1","type":"SOFT"}],`
由于列表索引0的值中的组合code + brc + des1
组合与索引1的值匹配,因此我们检查限制性最强的类型(在本例中为hard)。我们还在其他索引中检查相同的组合键,并给出如下的最终输出
output : `[{ "code": 1,"brc":"ABC","dest":"des1","type":"HARD"}],`
- 如果任何值中有多余的元素,而这些元素没有任何与键匹配的元素,我们必须返回它们。(以下输入列表中的示例是具有
code:5 and code: 6
的元素),它们是最终列表的一部分
下面是完整的输入,我们必须在列表中循环,从列表索引0中获取value
,并将该组合键与该列表中其他索引的所有值进行比较,并相应地合并它们。
let data = [{
"id":1,
"value":[
{
"code": 1,
"brc":"abc",
"dest":"des1",
"type":"SOFT"
},
{
"code": 2,
"brc":"def",
"dest":"des2",
"type":"HARD"
},
{
"code": 3,
"brc":"ghi",
"dest":"des3",
"type":"SOFT"
},
{
"code": 4,
"brc":"jkl",
"dest":"des4",
"type":"SOFT"
},
{
"code": 5,
"brc":"mno",
"dest":"des5",
"type":"SOFT"
}
]
},
{
"id":2,
"value":[
{
"code": 1,
"brc":"abc",
"dest":"des1",
"type":"HARD"
},
{
"code":2 ,
"brc":"def",
"dest":"des2",
"type":"HARD"
},
{
"code": 3,
"brc":"ghi",
"dest":"des3",
"type":"HARD"
},
{
"code": 4,
"brc":"jkl",
"dest":"des4",
"type":"SOFT"
},
{
"code": 6,
"brc":"pqr",
"dest":"des6",
"type":"SOFT"
}
]
},
{
"id":3,
"value":[
{
"code": 1,
"brc":"abc",
"dest":"des1",
"type":"SOFT"
},
{
"code": 2,
"brc":"def",
"dest":"des2",
"type":"HARD"
},
{
"code": 3,
"brc":"ghi",
"dest":"des3",
"type":"SOFT"
},
{
"code": 4,
"brc":"jkl",
"dest":"des4",
"type":"SOFT",
"extra": "shouldpersisit"
},
]
}
];
输出:
[{
"code": 1,
"brc":"abc",
"dest":"des1",
"type":"HARD"
},
{
"code": 2,
"brc":"def",
"dest":"des2",
"type":"HARD"
},
{
"code": 3,
"brc":"ghi",
"dest":"des3",
"type":"HARD"
},
{
"code": 4,
"brc":"jkl",
"dest":"des4",
"type":"SOFT"
},
{
"code": 4,
"brc":"jkl",
"dest":"des4",
"type":"SOFT"
},
{
"code": 5,
"brc":"mno",
"dest":"des5",
"type":"SOFT"
},
{
"code": 6,
"brc":"pqr",
"dest":"des6",
"type":"SOFT"
}
]
我已经写了伪代码,但我觉得我已经有4个循环了,这肯定会增加计算时间。(我还没有在这个函数中形成最终列表,但最终会返回上面所示的输入的输出)。
function doMerge(inputData){
inputData.forEach((data,index) => {
// loop through input list
data.value.forEach(innerData => {
// looping through the value key in the list and form the combination key for the first item in the value
const concatKey = `${innerData.code}${innerData.brc}${innerData.dest}`;
// loop through the total inputData again to go through each index's value and check for the key combination
// once key combination is found, find the 'type' of that object and save the most restricted out of the both.
// exit from that list ( hence using `some` )
// move on to all other items in the input Data .
inputData.forEach(allItems => {
let finalType = '';
allItems.value.some(item => {
const innerConcatKey = `${item.code}${item.brc}${item.dest}`;
if(concatKey === innerConcatKey){
finalType = loopDataInner.type === 'HARD' ? 'HARD' : 'SOFT';
return true;
}
});
});
});
});
}
doMerge(数据);
实现这一点的一种方法是使用字典来存储键/值,将它们存储在数组中。这会使代码更加复杂,在Javascript上,您可以通过两种方式使用字典:使用对象或Map对象。
我创建了一个代码来说明我的意思:
function doMerge(data = []) {
// We create a flat array only with the entries from value prop
const flattenedData = data.reduce((acc,el) => {
acc.push(...el.value);
return acc;
},[]);
//MergeData will be our main dict
const mergeData = new Map();
flattenedData.forEach(element => {
const {code,brc,dest,type} = element;
const key = `${code}-${brc}-${dest}`
if(mergeData.has(key)) {
if(type === 'HARD') {
//We only update if the type is HARD
mergeData.set(key,element);
}
} else {
mergeData.set(key,element);
}
});
const elements = [];
for (let value of mergeData.values()){
elements.push(value);
}
return elements;
}
let data = [{
"id":1,
"value":[
{
"code": 1,
"brc":"abc",
"dest":"des1",
"type":"SOFT"
},
{
"code": 2,
"brc":"def",
"dest":"des2",
"type":"HARD"
},
{
"code": 3,
"brc":"ghi",
"dest":"des3",
"type":"SOFT"
},
{
"code": 4,
"brc":"jkl",
"dest":"des4",
"type":"SOFT"
},
{
"code": 5,
"brc":"mno",
"dest":"des5",
"type":"SOFT"
}
]
},
{
"id":2,
"value":[
{
"code": 1,
"brc":"abc",
"dest":"des1",
"type":"HARD"
},
{
"code":2 ,
"brc":"def",
"dest":"des2",
"type":"HARD"
},
{
"code": 3,
"brc":"ghi",
"dest":"des3",
"type":"HARD"
},
{
"code": 4,
"brc":"jkl",
"dest":"des4",
"type":"SOFT"
},
{
"code": 6,
"brc":"pqr",
"dest":"des6",
"type":"SOFT"
}
]
},
{
"id":3,
"value":[
{
"code": 1,
"brc":"abc",
"dest":"des1",
"type":"SOFT"
},
{
"code": 2,
"brc":"def",
"dest":"des2",
"type":"HARD"
},
{
"code": 3,
"brc":"ghi",
"dest":"des3",
"type":"SOFT"
},
{
"code": 4,
"brc":"jkl",
"dest":"des4",
"type":"SOFT",
"extra": "shouldpersisit"
},
]
}
];
const result = doMerge(data);
console.log(result);
在那里你可以看到,我们需要三个阵列操作:
- 首先,我们只使用主数据中的值创建一个平面数组
- 我们遍历平面数组,如果它不存在,则存储该值,如果它存在,但新类型是HARD(因为它具有优先级),则更新该值
- 最后一件事是对Map对象进行迭代,以检索作为数组的数据