是否有标准定义了如何比较JavaScript,在Chrome控制台上我得到了这个
[10,0,0] > [1,0,0]
true
[10,0,0] > [5,0,0]
false
[5, 0, 0, 0] < [10, 0, 0, 0] //repeatable
false
[10,0,0,0] > [9,0,0,0]
false
[11,0,0,0] > [10,0,0,0]
true
Run Code Online (Sandbox Code Playgroud)
这是非常不显眼的,我甚至无法理解正在应用什么逻辑,并且它们看起来可重复,所以不基于对象id(ref)等,所以有没有任何文档呢?
假设我有2个阵列
firstArray = [1, 2, 3, 4, 5];
secondArray = [5, 4, 3, 2, 1];
Run Code Online (Sandbox Code Playgroud)
我想知道它们是否包含相同的元素,而顺序并不重要.我知道我可以编写一个函数来对它们进行排序,然后循环遍历它们进行检查,但是有没有预先构建的函数呢?(不仅是Vanilla JS,其他javascript库也没关系)
我需要存储点列表并检查该列表中是否已包含新点
class Point {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
window.onload = () => {
var points : Point[] = [];
points.push(new Point(1,1));
var point = new Point(1,1);
alert(points.indexOf(point)); // -1
}
Run Code Online (Sandbox Code Playgroud)
显然打字稿使用引用比较,但在这种情况下没有意义。在 Java 或 C# 中,我会equals在似乎不可能的打字稿中重载该方法。
我考虑过遍历数组foreach并检查每个条目是否相等,但这看起来相当复杂并且会使代码膨胀。
equals打字稿中有类似的东西吗?我怎样才能实现我自己的比较?
正如标题所说,我想比较两个 js 数组,其中我只关心内容是否相同,但我不关心它们的顺序是否相同。所以我期望的是:
[1, 2, 3, 3] == [1, 2, 3, 3] // True
[1, 2, 3, 3] == [1, 3, 2, 3] // True
[1, 2, 3, 3] == [1, 2] // False
[1, 2, 3, 3] == [1, 2, 3] // False
[1, 2, 3, 3] == [1, 2, 3, 3, 3] // False
[1, 2, 3, 3] == [1, "2, 3, 3"] // False
Run Code Online (Sandbox Code Playgroud)
显然比较运算符不起作用。从这个 SO answer我得到了下面的 Array.prototype 方法,但不幸的是,它也会检查顺序是否相同。
那么有人知道如何检查两个 js 数组是否包含相同的元素,而不考虑元素的顺序吗?欢迎所有提示!
Array.prototype.equals = function …Run Code Online (Sandbox Code Playgroud) 当最初的目的是创建一个新数组时,为什么map方法会改变原始数组?
我有一个对象数组,我传递给一个纯函数,然后映射给定的数组并返回一个新的.然后我注意到原始数组也被改变了.我理解Js中的Object通过引用传递的概念,但是仍然无法抓住为什么实现map会改变原始数组,有点胜过IMO的目的.
var initialArray = [ { name: 'one' }, { name: 'two' }, { name: 'three'} ];
function doSomething(array) {
// lodash
// return _.map(array, (item) => _.assign(item, {isSelected: true}));
// vanilla
return array.map(function(item) {
item['isSelected'] = true;
return item
});
}
var changedArray = doSomething(initialArray);
console.log('initialArray', initialArray); // [{ name: 'one', isSelected: true }, ...]
console.log('changedArray', changedArray); // [{ name: 'one', isSelected: true }, ...]
console.log(initialArray === changedArray); // false
Run Code Online (Sandbox Code Playgroud)
首先我想了解为什么会这样?
第二个我喜欢理解如何在不改变原始数组的情况下映射数组?(即._cloneDeep每次 …
例如
$ node
> [1, 2, 3] == [1, 2, 3]
false
Run Code Online (Sandbox Code Playgroud)
如果我错误地使用了“相同”和“等效”,请道歉。
$ node
> [1, 2, 3] == [1, 2, 3]
false
Run Code Online (Sandbox Code Playgroud)
我问,因为我习惯了 Ruby 等语言
$ irb
irb(main):001:0> [1,2,3] == [1,2,3]
=> true
Run Code Online (Sandbox Code Playgroud)
...或Python
$ python
>>> [1,2,3] == [1,2,3]
True
Run Code Online (Sandbox Code Playgroud)
...其中双等于是比较表达式的值
我有一个像这样的数组:
let x = [[1, 2], [3, 4], [1, 2], [2, 1]];
Run Code Online (Sandbox Code Playgroud)
我该怎么办才能检索没有重复项的数组?
[[1, 2], [3, 4], [2, 1]];
Run Code Online (Sandbox Code Playgroud)
我想使用过滤器方法。我试过了,但是没有用:
x.filter((value,index,self) => (self.indexOf(value) === index))
Run Code Online (Sandbox Code Playgroud)
编辑:正如我指定要使用过滤器方法,我不认为这个问题是重复的。另外,我得到了几个有趣的答案。
I couldn't find any description or any mention about how >, <, <= and >= operators behave while comparing two arrays in javascript.
The only trivial thing I could think of is that the two arrays are compared by both elements for each relative indexes, but after testing it - I didn't got the result I expected.
So how arrays are being compared?
Few test cases:
console.log([1] > [2]); // FALSE - ok
console.log([2] > [1]); // TRUE …Run Code Online (Sandbox Code Playgroud) const myArray = [
[2, 4], "cat", "hamster", 9
]
console.log(myArray.includes("cat"))
console.log(myArray.includes([2, 4]))Run Code Online (Sandbox Code Playgroud)
输出为真,为假。include() 不适用于数组内的数组吗?谢谢
我正在玩javaScript,发现一些令人困惑的东西..
var a = [1,2,3];
var b = [1,2,3];
var c = '1,2,3';
a==b // false
b==c // true
c==a // true
Run Code Online (Sandbox Code Playgroud)
幕后发生了什么?有谁知道吗?
javascript ×10
arrays ×8
comparison ×4
cmp ×1
immutability ×1
mapping ×1
mutable ×1
operators ×1
reference ×1
typescript ×1
web ×1