简体   繁体   English

如何比较两个 arrays 并在 js 中返回 true

[英]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我正在与两个 arrays 进行比较,如果元素的数组之一与元素的另一个数组匹配,则它应该返回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"]我使用了some ,但我得到了["ram"]的值

Use filter and includes and then check the length.使用filterincludes ,然后检查长度。

 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)

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

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