gpt4 book ai didi

angular - 为什么两个相等的对象在 Angular 2 中显示 'not equal"

转载 作者:太空狗 更新时间:2023-10-29 17:22:59 26 4
gpt4 key购买 nike

我从 json 文件上传数组。每 1.5 秒我检查文件是否有任何更改(目前我在一个文件上测试没有任何更改),但是当我检查是否

    if ( this.itemsParentArray[i] !== this.itemInArray[i] )

一直显示不相等,console.log(""not equal")

我是否遗漏了代码中的某些内容??这是:

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 ( this.itemsParentArray[i] !== this.itemInArray[i] ) {
// this.itemsParentArray[i] = this.itemInArray[i];
console.log("not equal");
}
}
}

//
ngOnInit() {
//makes http request and puts result into parentArray after 3 sec.
this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result));
}
//view items
viewNodes(result) {
setTimeout(() => {
this.myRes = result;
this.itemsParentArray = this.myRes.content;
this.showAssigned = true;
}, 3000);
}
}

如您所见,它从同一个文件加载数据(我不更改文件数据!!!):

this.itemsParentArray = this.myRes.content;

和(每 1.5 秒):

this.itemInArray = this.myRes.content;

最佳答案

== 尤其是 === 不检查对象是否包含具有相同值的相同属性。它只是检查它是否是相同的对象引用。

{} == {} 

{} === {}

也会导致 false

另见 Typescript: Avoid comparison by referenceHow do I initialize a typescript object with a JSON object

Plunker example

关于angular - 为什么两个相等的对象在 Angular 2 中显示 'not equal",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37785314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com