首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从两个对象数组中找到类似的项

从两个对象数组中找到类似的项
EN

Stack Overflow用户
提问于 2022-09-28 13:57:50
回答 3查看 37关注 0票数 0
代码语言:javascript
复制
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'}
]
代码语言:javascript
复制
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“

我预期的结果是:

代码语言:javascript
复制
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'}
]
EN

回答 3

Stack Overflow用户

发布于 2022-09-28 14:20:37

//希望这能帮到你

代码语言:javascript
复制
// 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");
    }
票数 0
EN

Stack Overflow用户

发布于 2022-09-28 14:28:16

代码语言:javascript
复制
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、约简等。

票数 0
EN

Stack Overflow用户

发布于 2022-09-29 07:15:55

您只需使用下面的黑客就可以实现此要求。

  • array1创建一个字符串数组,其中将包含值(服务+模块)。

arrayOfStrings = array1.map(obj => obj.service + obj.module);

array2.map((obj,index) => { obj.type = (arrayOfStrings.includes(obj.service + obj.module))“多”:‘单’;返回obj;});

现场演示

代码语言:javascript
复制
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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73882747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档