可能我们会直接想到使用 == 或者 === ,但其实这样是不能得到结果的,一致判定为 flase 。
console.log([1, 1] == [1, 1]) // false
console.log([1, 1] === [1, 1]) // false
console.log([{}] == [{}]) // false
console.log([{}] === [{}]) // false
console.log({} == {}) // false
console.log({} === {}) // false
1、要判断数组是否相等的话可以使用 .toString() 或 .join() 方法,这两个方法都是将数组转换为字符串,可以进行判断。
console.log([1, 1].toString() == [1, 1].toString()) // true
console.log([1, 1].join() === [1, 1].join()) // true
console.log([1, 1].toString() == [2, 1].toString()) // false
console.log([1, 1].join() === [2, 1].join()) // false
// 以上两个的判断是没有问题的
console.log([{name: '张三'}].toString() == [{name: '李四'}].toString()) // true
console.log({name: '张三'}.join() == {name: &