[英]how to decrease the values of array1 in relation to array2 in javascript
[英]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,從 Object 2 中刪除所有其他 property_names 並保留匹配的那些。
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
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, ]
預期結果
因此,目標是讓 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']
謝謝
每次迭代第一個數組中的元素時,循環遍歷第二個數組,然后檢查匹配項。
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.