简体   繁体   English

如何将 Array1 中的元素与 javascript 中 Array2 的每个元素进行比较并返回匹配值

[英]How to compare element in Array1 with each each elements of Array2 in javascript and return matched values

Here I have two objects, and I would like to compare each element of Object 1 with each element of Object 2. Note that the elements of object 1 constitute the property_names of object 2. And if the property_names of Object 1 exists in Object 2, remove all other property_names from Object 2 and keep the ones that matched. Here I have two objects, and I would like to compare each element of Object 1 with each element of Object 2. Note that the elements of object 1 constitute the property_names of object 2. And if the property_names of Object 1 exists in Object 2,从 Object 2 中删除所有其他 property_names 并保留匹配的那些。

Object 1 Object 1

[0: {name: 'Id', match: 'id', matched: true}
1: {name: 'Driver', match: 'fullName', matched: true}
2: {name: 'Sex', match: 'gender', matched: true} ]

Object 2 Object 2

0: [Id: '1256', Driver: 'Jack Burn', Sexe: 'M', Age: '32', Situation: 'ACTIF', …]
1: [Id: '1896', Driver: 'Sacks Maddison', Sexe: 'M', Age: '25', Situation: 'ABANDONS', …]
2: [Id: '1001', Driver: 'Hilary Michael', Sexe: 'F', Age:'21', Situation: 'ACTIF', Téléphone: 
xxxx, ]

The expected result预期结果

So the objective is to have object 2 with only the attributes of object 1 that match:因此,目标是让 object 2 仅具有匹配的 object 1 的属性:

 0: [Id: '1256', Driver: 'Jack Burn', Sexe: 'M']
 1: [Id: '1896', Driver: 'Sacks Maddison', Sexe: 'M']
 2: [Id: '1001', Driver: 'Hilary Michael', Sexe: 'F']

Thanks谢谢

Loop through the second array each time you iterate over an element in the first array, then check for matches.每次迭代第一个数组中的元素时,循环遍历第二个数组,然后检查匹配项。

var array1 = ["cat", "sum", "fun", "run"],
    array2 = ["bat", "cat", "dog", "sun", "hut", "gut"];

function getMatch(a, b) {
    var matches = [];

    for ( var i = 0; i < a.length; i++ ) {
        for ( var e = 0; e < b.length; e++ ) {
            if ( a[i] === b[e] ) matches.push( a[i] );
        }
    }
    return matches;
}

getMatch(array1, array2); // ["cat"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 javascript 中减少 array1 相对于 array2 的值 - how to decrease the values ​of array1 in relation to array2 in javascript 如何在 array2 中查找不在 array1 中的元素? - How to find elements in array2 that are not in array1? 对于 Array1 中的每 7 个元素,添加一个 Array2 中的元素 - For every 7 elements from Array1, add an element from Array2 我有两个包含其他数组的数组。 如何将array1中的每个嵌套数组与array2中的对应数组进行比较? - I have two arrays which contain other arrays. How can I compare each nested array in array1 with its counterpart in array2? 如何将array1转换为array2? - How to convert array1 to array2? 检查array1是否包含在array2中,元素的顺序完全相同? - Check if array1 is contained in array2 with elements in exactly the same order? Javascript:array1 [0] = array2 [0]; 这是通过价值还是通过参考? - Javascript: array1[0] = array2[0]; does this pass by value or by reference? 通过引用 array2 从 array1 中删除值 - Remove the values from array1 by referring to array2 如何使用splice正确地将array2连接到array1 - how to concat array2 to array1 correctly with splice 如何以array1的第一个元素和array2的第一个元素等方式组合2个数组形成一个新数组 - How to combine 2 arrays in a way that 1st element of array1 and 1st element of array2 and so on form a new array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM