I have
arr1 = [aaa,bbb,ccc,ttt,yyy]
arr2 = [abc,ttt,def,hij,ew,y,uuu]
I want the same/common items in both arrays to be saved in a new array
Same_items = [ttt]
I have tried using
var Same_items = [];
for(let i = 0; i < arr1.length; i++) {
if(arr2.indexOf(arr1[i]) !== -1) {
Same_items.push(arr1[i]);
}
}
also,
var Same_itema = [];
Same_items = arr1.filter(item => arr2.includes(item));
I not getting the desired output. I am not doing it right. Please guide
Thank you!
const arr1 = ["aaa", "bbb", "ccc", "ttt", "yyy"];andconst arr2 = ["abc", "ttt", "def", "hij", "ew", "y", "uuu"];are the sample input values,Same_itemsofSame_items = arr1.filter(item => arr2.includes(item));will return the value of["ttt"]. So, can you provide the detail ofI not getting the desired output.? For example, how aboutconsole.log(["aaa", "bbb", "ccc", "ttt", "yyy"].filter(item => ["abc", "ttt", "def", "hij", "ew", "y", "uuu"].includes(item)))? Also, I think that your above script returns["ttt"].