[Vuejs]-Return only some object fields in an array using Vue.js

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.com/a/25470077/6923146

You can use map:

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

3πŸ‘

you can Try this also

<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>```

2πŸ‘

Try to use map array function as follows :

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

Leave a comment