【问题标题】:Return only some object fields in an array using Vue.js使用 Vue.js 仅返回数组中的一些对象字段
【发布时间】:2020-03-10 07:00:38
【问题描述】:

所以我有来自 api 请求的数组中的这些对象

(5) [{…}, {…}, {…}, {…}, {…}]
0: {ID: 1, Title: "dqdq", Description: "dqdq", IdUser: 3}
1: {ID: 2, Title: "dqdq", Description: "dqdq", IdUser: 3}
2: {ID: 3, Title: "dqdqddd", Description: "dqdq", IdUser: 3}
3: {ID: 4, Title: "dqdqddd", Description: "dqdq", IdUser: 3}
4: {ID: 5, Title: "dqdqddd", Description: "dqdq", IdUser: 3}
length: 5

我需要的是只取标题,这样我就可以在 div 中循环它,以此类推作为描述并将它传递给子组件,这样我就可以在 html 部分循环它

<template>
<div>
<Navbar />
<gamesIdeaList :games="games" />
</div>

</template>

<script>
// @ is an alias to /src
import Navbar from '@/components/Navbar.vue'
import gamesIdeaList from '@/components/gamesIdeaList.vue'
import axios from 'axios'
export default {
  name: 'home',
  components: {
    Navbar,
    gamesIdeaList
  },
  data() {
      return {
          games:[]
      }
  },
  //  props: info,
   mounted () {
    axios.get('http://localhost:3001/GamesIdea', {
        headers: { Authorization: `Bearer ${localStorage.usertoken}` }
      })
      .then(res => {
 this.games = res.data.data

console.log(arr)
      })
      .catch(error => {
        console.log(error)
        this.errored = true
      })
}
}
</script>

【问题讨论】:

  • 请试试这个this.games = res.data.data.map(d=&gt;{Title: d.Title,Description:d.Description});

标签: javascript vue.js vuejs2 axios vue-component


【解决方案1】:

可以添加你不想要的字段作为回报

解决方案 1

this.games = res.data.data.map(({
    ID,
    IdUser,
    ...rest
}) => rest);

var v1 = [
{ID: 1, Title: "dqdq", Description: "dqdq", IdUser: 3},
{ID: 2, Title: "dqdq", Description: "dqdq", IdUser: 3},
{ID: 3, Title: "dqdqddd", Description: "dqdq", IdUser: 3},
{ID: 4, Title: "dqdqddd", Description: "dqdq", IdUser: 3},
{ID: 5, Title: "dqdqddd", Description: "dqdq", IdUser: 3}]

var games = v1.map(({
    ID,
    IdUser, ...rest}) => rest);

console.log(games);

解决方案 2

https://stackoverflow.com/a/25470077/6923146

你可以使用map:

this.games = res.data.data.map(obj => {
    Title: obj.Title,
    Description: obj.Description
});

【讨论】:

  • @anabel 请检查答案,它已更新为新解决方案
  • 它的工作提供的所有解决方案都在工作,非常感谢
  • @anabel,如果它适合你,请接受我的回答。谢谢。
【解决方案2】:

你也可以试试这个

<div>
<Navbar />
<gamesIdeaList :games="games" />
</div>

</template>

<script>
// @ is an alias to /src
import Navbar from '@/components/Navbar.vue'
import gamesIdeaList from '@/components/gamesIdeaList.vue'
import axios from 'axios'
export default {
  name: 'home',
  components: {
    Navbar,
    gamesIdeaList
  },
  data() {
      return {
          games:[]
      }
  },
  //  props: info,
   mounted () {
    axios.get('http://localhost:3001/GamesIdea', {
        headers: { Authorization: `Bearer ${localStorage.usertoken}` }
      })
      .then(res => {
 this.games = res.data.map(val => {
return {
Title: val.Title,
Description:val.Description}
})

console.log(arr)
      })
      .catch(error => {
        console.log(error)
        this.errored = true
      })
}
}
</script>```

【讨论】:

    【解决方案3】:

    尝试使用map数组函数如下:

     this.games = res.data.data.map(d=>{Title: d.Title,Description:d.Description}); 
    

    【讨论】:

    • 确实有效,所以我传递了两个道具,一个用于标题,一个用于描述?
    • 是否要将标题和描述都传递给子组件?
    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    相关资源
    最近更新 更多