简体   繁体   中英

how to compare two arrays and return true in js

I'm comparing with the two arrays and if one of the array of the element matches with the other array of the element it should return the true

var data1 = ["ram","krishna"]
var data2 = ["", "ram"]
const isInArray = dat1.includes(data2)  

I used the some but i'm getting the value of ["ram"]

Use filter and includes and then check the length.

 var data1 = ["ram","krishna"] var data2 = ["", "ram"] const isInArray = data1.filter(value => data2.includes(value)).length? true: false; console.log(isInArray);

 var data1 = ["ram","krishna"] var data2 = ["", "ram"] const isInArray = data1.some((elem) => data2.includes(elem)) console.log(isInArray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM