最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return only some object fields in an array using Vue.js - Stack Overflow

programmeradmin1浏览0评论

So i have those objects in array from an api request

(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

what i need to have is to take just the Title so i can loop it inside a div and so on for the description and pass it to the child ponent so i can loop it in the html part

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

</template>

<script>
// @ is an alias to /src
import Navbar from '@/ponents/Navbar.vue'
import gamesIdeaList from '@/ponents/gamesIdeaList.vue'
import axios from 'axios'
export default {
  name: 'home',
  ponents: {
    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>

So i have those objects in array from an api request

(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

what i need to have is to take just the Title so i can loop it inside a div and so on for the description and pass it to the child ponent so i can loop it in the html part

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

</template>

<script>
// @ is an alias to /src
import Navbar from '@/ponents/Navbar.vue'
import gamesIdeaList from '@/ponents/gamesIdeaList.vue'
import axios from 'axios'
export default {
  name: 'home',
  ponents: {
    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>
Share Improve this question edited Aug 4, 2020 at 14:13 Boussadjra Brahim 1 asked Nov 14, 2019 at 10:37 ana belana bel 1872 silver badges16 bronze badges 1
  • Please try this this.games = res.data.data.map(d=>{Title: d.Title,Description:d.Description}); – Hardik Masalawala Commented Nov 14, 2019 at 10:55
Add a ment  | 

3 Answers 3

Reset to default 3

Yes can add field you don't want in return

Solution 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);

Solution 2

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

You can use map:

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

you can Try this also

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

</template>

<script>
// @ is an alias to /src
import Navbar from '@/ponents/Navbar.vue'
import gamesIdeaList from '@/ponents/gamesIdeaList.vue'
import axios from 'axios'
export default {
  name: 'home',
  ponents: {
    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>```

Try to use map array function as follows :

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

评论列表(0)

  1. 暂无评论