const array1 = [
{name: 'bc', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycb', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycn', module: 'ksd', cluster: 'opc2'}
]const array2 = [
{name: 'bcl', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcc', service: 'ycb', module: 'ksd', cluster: 'opcw'},
{name: 'bca', service: 'ycb', module: 'ksd', cluster: 'opcm'},
{name: 'bcv', service: 'ycr', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycq', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycx', module: 'ksd', cluster: 'opc2'}
]我想用两个键来比较项目:服务+模块。如果有类似的项目,添加键"type":"Multi",如果没有,则添加"type":"Single“
我预期的结果是:
result = [
{name: 'bc', service: 'yc', module: 'ksd', cluster: 'opc', type: 'Multi'},
{name: 'bcl', service: 'yc', module: 'ksd', cluster: 'opc', type: 'Multi'},
{name: 'bcc', service: 'ycb', module: 'ksd', cluster: 'opcw', type: 'Multi'},
{name: 'bca', service: 'ycn', module: 'ksd', cluster: 'opc2', type: 'Single'}
]发布于 2022-09-28 14:20:37
//希望这能帮到你
// Declare two array
const array1 = ['a', 'b', 'c', 'd'];
const array2 = ['k', 'x', 'z'];
// Function definition with passing two arrays
function findCommonElement(array1, array2) {
// Loop for array1
for(let i = 0; i < array1.length; i++) {
// Loop for array2
for(let j = 0; j < array2.length; j++) {
// Compare the element of each and
// every element from both of the
// arrays
if(array1[i] === array2[j]) {
// Return if common element found
return console.log(array1[i]);
}
}
}
// Return if no common element exist
return console.log("sorry no common element found");
}发布于 2022-09-28 14:28:16
const arr3 = [];
const array1 = [
{name: 'bc', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycb', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycn', module: 'ksd', cluster: 'opc2'}
]
const array2 = [
{name: 'bcl', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcc', service: 'ycb', module: 'ksd', cluster: 'opcw'},
{name: 'bca', service: 'ycb', module: 'ksd', cluster: 'opcm'},
{name: 'bcv', service: 'ycr', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycq', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycx', module: 'ksd', cluster: 'opc2'}
]
array1.forEach((item)=>{
array2.forEach((item2) => {
if((item['module'] === item2['module']) && (item['service'] === item2['service'])){
item.type = 'multi';
arr3.push(item);
}
});
if(!item.type) {
item.type ='single';
arr3.push(item);
}
});
console.log(arr3);
您可以使用更高阶的函数来优化解决方案,例如过滤器、map、约简等。
发布于 2022-09-29 07:15:55
您只需使用下面的黑客就可以实现此要求。
array1创建一个字符串数组,其中将包含值(服务+模块)。arrayOfStrings = array1.map(obj => obj.service + obj.module);
Array.map()和Array.includes()的帮助下添加属性。array2.map((obj,index) => { obj.type = (arrayOfStrings.includes(obj.service + obj.module))“多”:‘单’;返回obj;});
现场演示
const array1 = [
{name: 'bc', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycb', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycn', module: 'ksd', cluster: 'opc2'}
];
const array2 = [
{name: 'bcl', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcc', service: 'ycb', module: 'ksd', cluster: 'opcw'},
{name: 'bca', service: 'ycb', module: 'ksd', cluster: 'opcm'},
{name: 'bcv', service: 'ycr', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycq', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycx', module: 'ksd', cluster: 'opc2'}
];
const array1Arr = array1.map(obj => obj.service + obj.module);
const res = array2.map((obj, index) => {
obj.type = (array1Arr.includes(obj.service + obj.module))? 'Multi' : 'Single';
return obj;
});
console.log(res);
https://stackoverflow.com/questions/73882747
复制相似问题