为了账号安全,请及时绑定邮箱和手机立即绑定

使用 .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 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号