首页 > 解决方案 > Vue中对象属性与字符串的比较失败

问题描述

结论:这是一个错字。

我正在尝试一些看起来很简单的东西,但我认为我错过了一些对 Vue 至关重要的东西。我有一个 id,以及一个具有一次子属性的对象,其中包含具有该 id 的对象。当我遍历对象属性以比较 id 时,它是错误的。当我在比较之后记录在 else 语句中比较的值时,它们在控制台日志中似乎是相同的。

无论我尝试什么,我总是以“坏包”行结束。控制台向我显示 object 属性在那里,并且该属性的对象 id 是正确的。看起来像:

bad package c5186ae5-1558-490b-a9d2-5fc19c0a736d !== c5186ae5-1588-490b-a9d2-5fc19c0a736d

我怀疑这与 Vue 中的可观察对象有关,即使日志没有显示任何可观察对象。

做我想做的事情的正确方法是什么?

// newVal = '29e51254-a88a-4309-92bc-ad1d0cda1427'
collectionIdForVerification (newVal) {
         const parcelId = this.$store.state.parcels.collectionIdForVerification
         // this print the object, it looks good
         console.log('Parcels are ', this.requestedParcelsForCollection)
         console.log('local parcels are ', this.local.parcels)
         const loParcOb = JSON.parse(JSON.stringify(this.local.parcels))
         console.log('local parcels are ', loParcOb)
         console.log('does the parcel for ', newVal, ' exist? ', this.local.parcels[newVal])
         if (newVal) {
           const packages = loParcOb
           console.log('lets check ', packages, ' for ', parcelId)
           const rightPackage = loParcOb[parcelId]
           console.log('right package is ', rightPackage)
           for (let x in loParcOb) {
             let ob = loParcOb[x]
             console.log('object: ', ob)
             if (ob.id.toString() === parcelId) {
               console.log('good pagage ', ob)
             } else {
               // this shows me that the two compared things are equal
               console.log('bad package ', ob.id.toString(), ' !== ', parcelId)
             }
           }
         }
       }

目的:

// object
{
displayImage: "https://static.xxxx.co/couriers/dhl/dhl_detail.png"
expiration: 1573486971237
id: "29e51254-a88a-4309-92bc-ad1d0cda1427"
name: "MacBook from YYYY"
passcode: ""
recipient: {xxxId: "0cbf8068-a286-49c5-bdf7-7509ac953b5f", yyyId: "b925b763-992e-4ad3-98b1-ba8fba2ef37e"}
sender: {xxxId: "9cad32d0-6724-414b-a686-a8e133db2225", yyyId: "997eeab8-a698-4027-93d3-43ca5d6ee121"}
slotHeight: 2
startingSlot: 37
status: "IN_STATION"
trackingNumber: "A123456002"
}

标签: vue.jsvuejs2vuex

解决方案


id不一样。

一个包含1558. 另一个有1588


推荐阅读