js Double for loop nesting, find the data with the same attribute value in the two arrays, and add it to the new array

js double for loop nesting, find the data whose group and id in arr2 are the same as the attribute values ​​in arr1, and add it to the new array

let arr1 = [{group: 1, id: 2, name: "aa"}, {group: 2, id: 1, name: "bb"}, {group: 2, id: 2, name: "cc"}, {group: 3, id: 2, name: "dd"}];
let arr2 = [{group: 1, id: 1, name: "ee"}, {group: 2, id: 2, name: "ff"}, {group: 3, id: 3, name: "gg"}, {group: 4, id: 2, name: "hh"}];

let arr3 = [];
for (let i = 0; i < arr1.length; i++) {
    let pre = arr1[i];
    for (let j = 0; j < arr2.length; j++) {
        let old = arr2[j];
        if (pre.group == old.group && pre.id == old.id) {
            arr3.push(old);
        }
    }
}
console.log(arr3);

Guess you like

Origin blog.csdn.net/qq_40015157/article/details/113868943