After obtaining the NEXT-Data data from the AppStore, the HarmonyOS NEXT-Data data is found to be the same data (with the same value), but the compari

1 min read
I'm using
dataTime.indexOf(Time)
来在Date[]数组里面找一个存在的相同的数据,
发现即使我明确知道该dataTiem中有和Time一样的Data属性的值,
但是却获得了-1,比较结果为false。
Analyze the cause of the error:
Date[] is a complex data type, and when comparing, it will compare whether their addresses are the same, but not their values, so how do you do that? to compare their values.
The sample code is as follows:
Method 1: Use JSON
@Entry
@Component
struct Index {
@State dataTime: Array<Date> = []
@State Time: Date = new Date()
aboutToAppear(): void {
this.dataTime.forEach((item)=>{
console.log(''+Boolean(JSON.stringify(item)===JSON.stringify(this.Time)))
})
}
build() {
}
}
Method 2: Use the original method to compare
@Entry
@Component
struct Index {
@State dataTime: Array<Date> = []
@State Time: Date = new Date()
aboutToAppear(): void {
this.dataTime.forEach((item)=>{
console.log(Boolean(item.getTime()=== this.Time.getTime())+'')
})
}
build() {
}
}
0
Subscribe to my newsletter
Read articles from xiao directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
