我想比较两个阵列......理想情况下,有效率.没有什么花哨的,只要true它们是相同的,false如果不相同的话.毫不奇怪,比较运算符似乎不起作用.
var a1 = [1,2,3];
var a2 = [1,2,3];
console.log(a1==a2); // Returns false
console.log(JSON.stringify(a1)==JSON.stringify(a2)); // Returns true
Run Code Online (Sandbox Code Playgroud)
每个数组的JSON编码都有,但有没有更快或更"简单"的方法来简单地比较数组而不必迭代每个值?
请看下面的脚本.我正在使用Chrome进行测试.
/*declare a new set*/
var items = new Set()
/*add an array by declaring as array type*/
var arr = [1,2,3,4];
items.add(arr);
/*print items*/
console.log(items); // Set {[1, 2, 3, 4]}
/*add an array directly as argument*/
items.add([5,6,7,8]);
/*print items*/
console.log(items); // Set {[1, 2, 3, 4], [5, 6, 7, 8]}
/*print type of items stored in Set*/
for (let item of items) console.log(typeof item); //object, object
/*check if item has array we declared as array type*/
console.log(items.has(arr)); // …Run Code Online (Sandbox Code Playgroud) 我从json文件上传数组.每1.5秒我检查文件是否有任何变化(目前我在一个文件上测试没有任何变化),但当我检查是否
if ( this.itemsParentArray[i] !== this.itemInArray[i] )
Run Code Online (Sandbox Code Playgroud)
它始终显示它不相等,并且console.log(""不等于")
我错过了代码中的内容吗?这里是:
export class HomeComponent {
itemsParentArray = [];
itemInArray = [];
myRes: Content;
showAssigned:boolean = false;
constructor(private structureRequest: StructureRequestService) {
setInterval(() => {
this.timerGetStructure();
}, 1500);
}
// using with setInterval to get new data and sets into content Array with this.updateItems(result) if it's new
timerGetStructure() {
this.structureRequest.sendRequest().subscribe((result) => this.updateItems(result));
}
updateItems(result) {
this.myRes = result;
this.itemInArray = this.myRes.content;
for ( let i = 0; i < this.itemInArray.length; i++) {
if ( …Run Code Online (Sandbox Code Playgroud)