There are two array objects of AB. If An and B intersect, insert them into the C array. If there is no intersection, insert a null value in the same position of A to C.

related codes

arr1 = [
    {
        id: 1001,
        name: ""
    },
    {
        id: 1002,
        name: ""
    },
    {
        id: 1003,
        name: ""
    }
]

arr2 = [
    {
        ids: 1001,
        age: 18
    },
    {
        ids: 1003,
        age: 20
    }
]

arr3 = []

topic description

arr2idsarr1id,arr3arr2"ids" arr1"id" nameage
arr3arr1nameage
Jun.30,2021

Union, intersection, difference can be calculated by Set

const arr1 = [
    {
        id: 1001,
        name: ''
    },
    {
        id: 1002,
        name: ''
    },
    {
        id: 1003,
        name: ''
    }
];
const arr2 = [
    {
        ids: 1001,
        age: 18
    },
    {
        ids: 1003,
        age: 20
    }
];
const arr3 = [];
const id_arr1 = new Set(arr1.map(o=>o.id));
const id_arr2 = new Set(arr2.map(o=>o.ids));
//id
const allIds = new Set([...id_arr1].filter(x=>id_arr2.has(x)))
arr1.forEach(o=>{
    //
    if(allIds.has(o.id)){
        arr3.push({name:o.name,age:arr2.find(o2=>o2.ids === o.id).age})
    }else{
        arr3.push({name:null,age:null})
    }
});
//
console.log(arr3);

clipboard.png

are you satisfied

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e61058-458f6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e61058-458f6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?