select one object from one json file by id vue

自闭症网瘾萝莉.ら 提交于 2019-12-06 12:20:43

this is more of a Javascript question, rather than a Vue question.

when your goal is to select one object from an array of object, .filter() is your friends.

for e.g. https://jsfiddle.net/jacobgoh101/1nv5cesv/3/

if the id you are targeting is 3

axios.get('https://jsonplaceholder.typicode.com/posts/').then(res=>{
  const data = res.data;
    const sampleId = 3;
  const post = data.filter((obj)=>{
    return obj.id === sampleId;
  }).pop();
  console.log(post);
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!