How to Find Matching Values in Two Arrays

Find matching values in two arrays

You can use the combination of map and includes helper to achieve that.

let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];
let jsonArray = [];

jsonArray = firstArray.map(i => {
return { 'name': i, 'matched': secondArray.includes(i) };
});

console.log(jsonArray);

Filtering two arrays for matching values into new array in Javascript

const newArr = strArr2.filter(value => strArr1.includes(value) && arr.splice(arr.indexOf(value), 1));

This works :)

finding matching value in two arrays or lists using loop

res = [key for key, val in enumerate(data) if val in data1]
print(res)

Output:

[1, 2, 4, 5]

Javascript compare 2 arrays of objects find matching values and make change

It seems your regex is removing the email address from the from property, rather than preserving it. Here's a sample with a different regex that pulls out the email during comparison and ignores the rest.

const nickNames = [
{
nickName:"Dad",
emailAddress:"dad@dad.com",
},
{
nickName:"Mom",
emailAddress:"mom@mom.com",
},
{
nickName:"BFF",
emailAddress:"bff@bff.com",
}
]

let emails = [
{
from:"Dad Dadson <dad@dad.com>"
},
{
from:"Mom Dadson <mom@mom.com>"
},
{
from:"Brother Dadson <bro@bro.com>"
}
]

emails.forEach(email => {
const match = nickNames.find(nickName => nickName.emailAddress === email.from.replace(/^(.* )<(.+)>$/, "$2"));
if (match) {
email.nickName = match.nickName;
}
});
console.log(emails);

finding matching values in two arrays

for (var lo = 0; lo < LocalJSONObject.length; ++lo) {
for (var va = 0; va < value.length; ++va) {
if (value[va] === LocalJSONObject[lo].value) {
console.log('Matching values:', value[va]);
}
}
}


Related Topics



Leave a reply



Submit