阵列比较失败 [英] Array comparison failure

查看:46
本文介绍了阵列比较失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说.为什么

 console.log(obj.hello[0].w == ['hi','hi']); // false

在以下内容中:

var obj = {
'hello':[
    {'w':['hi','hi']}
    ]
}
console.log(obj.hello[0].w);                // ['hi','hi']
console.log(obj.hello[0].w == ['hi','hi']); // false ??? Why is it false?
console.log(obj.hello[0].w[0] == 'hi');     // true
console.log(obj.hello[0].w[0] == ['hi']);   // true
console.log(obj.hello[0].w[0] === ['hi']);  // false
console.log(obj.hello[0].w[0] === 'hi');    // true

如果 obj.hello [0] .w!= ['hi','hi'] ,那么 obj.hello [0]的实际"值是多少.w ?

我首先认为问题与JSON有关,但事实证明,这与比较对象有关.抱歉,要重复.

I first thought the problem was about JSON but it turned out it's about comparing objects. Sorry, for duplicate.

推荐答案

您不能像简单变量一样比较数组.您正在将引用与两个不同的数组进行比较,无论其内容如何,​​该引用始终为false.

You cannot compare arrays like if they were simple variables. You're comparing references to two different arrays, and that will always be false, regardless of their contents.

如果要进行检查,则必须分别比较每个值.

If you want to do the check, you will have to compare each value individually.

这篇关于阵列比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆