如何查看数组的数组索引

问题描述 投票:0回答:1

例如我有这些数组

total = [[0,1],[0,2]]

arr = [0,2]

显然

total
包括
arr

但是

total.indexOf(arr)
返回
-1

有什么好的办法解决这个问题吗?

javascript
1个回答
0
投票

尝试

JSON.stringify

const total = [[0,1],[0,2]]

const arr = [0,2]

const index = total.findIndex(item => JSON.stringify(item) === JSON.stringify(arr))

console.log(index)

© www.soinside.com 2019 - 2024. All rights reserved.