我正在使用 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);? ? ? ??? ? }
使用 .find 方法不會從解析的 JSON 返回對象
qq_笑_17
2023-06-29 22:34:46