相关疑难解决方法(0)

如何在JavaScript中比较数组?

我想比较两个阵列......理想情况下,有效率.没有什么花哨的,只要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编码都有,但有没有更快或更"简单"的方法来简单地比较数组而不必迭代每个值?

javascript arrays json

894
推荐指数
24
解决办法
81万
查看次数

ES6 Set允许重复的数组/对象

请看下面的脚本.我正在使用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)

javascript ecmascript-6

8
推荐指数
1
解决办法
5813
查看次数

为什么两个相等的对象在Angular 2中显示"不相等"

我从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)

angular

6
推荐指数
1
解决办法
9676
查看次数

标签 统计

javascript ×2

angular ×1

arrays ×1

ecmascript-6 ×1

json ×1