简体   繁体   中英

Array of arrays contains array javascript

this might be a duplicate but I couldn't find a solution. If I have an array of arrays, is there a "built-in" way to determine if that array of arrays includes another array. Array.prototype.includes() helps, but doesn't get me all the way because of (I think) object references as shown below. I can manually test equality of each value but I'm sure there's a better way.

$ node
> b = [[1,2,3]]
[ [ 1, 2, 3 ] ]
> b[0]
[ 1, 2, 3 ]
> b.includes(b[0])
true
> b.includes([1,2,3])
false
> 

I recommend to use JSON.stringify & includes .

JSON.stringify([[1,2,3]]).includes(JSON.stringify([1,2,3]))
true
JSON.stringify([[1,3]]).includes(JSON.stringify([1,2,3]))
false

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