本文介绍了为什么要使用JavaScript推阵列和定义的数组不等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下。很迷茫,他们是否不相等。

I did the following. Very confused whether they are not equal.

var arr1 = []
arr1.push(1)
arr1.push(2)
arr1.push(3)

var arr2 = [1, 2, 3]

var isSame = (arr1==arr2)

为什么 isSame 假的?很困惑这个...

Why is isSame false? Very confused about this...

推荐答案

比较变量的时候,你才会比较变量本身,而不是它们的内容。

when comparing variables you will only compare the variables itself and not their content.

字符串,数字藏汉以布尔是简单类型。他们总是重新presenting自己的价值。

strings, numbers aswell as booleans are simple types. they are always representing their value.

对象不同。如果你比较对象,你只检查一个对象引用等于第二个比较的对象引用。

objects are different. if you compare objects, you will just check if an objects reference is equal to the objects reference of the second comparator.

通过比较对象,你永远不会比它们所包含的变量。
对象是简单的,甚至更多的变量容器。
变量可以是简单类型藏汉作为对象

by comparing objects you will never ever compare the variables they contain.objects are simply containers for even more variables.variables can be simple types aswell as objects.

你简单地比较两个基金会弄清楚,如果他们是完全一样的。

you are simply comparing two foundations to figure out if they are exactly the same.

如果你真的想要一个数组的内容比较,你需要检查递归包含藏汉每个变量另一个数组的内容。

if you really want to compare the content of an array to the content of another array you need to recursively check every variable it contains aswell.

一个例子..

var obj = {};

obj.foo = "bar";

(function (o) {
    o.foo = "baz";
})(obj);

console.log(o.foo); // will print "baz"

使用数组做

当它,你将得到同样的结果。

when doing it with arrays you will get the same result.

你可能想看看这个问题:藏汉为所有重复的

you might want to check out this question: Deep comparison of objects/arrays aswell as all of the duplicates

这篇关于为什么要使用JavaScript推阵列和定义的数组不等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:06