亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 .find 方法不會從解析的 JSON 返回對象

使用 .find 方法不會從解析的 JSON 返回對象

qq_笑_17 2023-06-29 22:34:46
我正在使用 Vue.js 3,但我認為這與我的問題無關。我調用 localStorage 來獲取帖子對象的 JSON 數組,對其進行解析,然后使用 id 來查找帖子。我從路線中獲取 id 沒有問題,但使用 .find 搜索解析的數組返回未定義。Post.vue 文件? ? created() {? ? ? ? this.postId = this.$route.params.id;? ? ? ? console.log("******? ? POST ID *********");? ? ? ? console.log(this.postId);? ? ? ? var posts = JSON.parse(localStorage.getItem("posts"));? ? ? ? console.log("******? ? POSTS ARRAY *********");? ? ? ? console.log(posts);? ? ? ? this.post = posts.find(post => post.id === this.postId);? ? ? ? console.log("****** POST *********");? ? ? ? console.log(this.post);? ? ? ??? ? }
查看完整描述

1 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

類型this.postId為字符串,數組中每個對象的id類型posts為數字。所以你可以使用==代替===. 或者使用 將 的類型更改this.postId為數字parseInt。



查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號