Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

80
Views
Vue pass data into array specific item name

Hey I am really new to Vue and for this project I was trying to add data inside array using .push() . When I push data inside totalPlayers its suppose to get data as totalPlayers[{data:[0,1,2,3]}] but it currently saves data as totalPlayers[{data: [] }, 0, 1, 2, 3]. Is there a way to fix this? Here is my code below

JsFiddle = https://jsfiddle.net/ujjumaki/xv2homt8/24/

Method

new Vue({
  el: "#app",
  data: {
    totalPlayers:[{
     data:[],
    }],
    playerList:4,
  },
  methods: {
    buttonClicked(){
        for (var i = 0; i < this.playerList; i++) {
        console.log('i was '+i);
        this.totalPlayers.push(i);
        console.log(this.totalPlayers);
      }
    }
  }
})

View

<div id="app">
  <button @click="buttonClicked()">
    Click Me
  </button>
</div>
10 months ago · Santiago Trujillo
2 answers
Answer question

0

data is array in first element of totalPlayers array, so try totalPlayers[0].data.push:

new Vue({
  el: "#app",
  data: {
    totalPlayers:[{
     data:[],
    }],
    playerList:4,
  },
  methods: {
    buttonClicked(){
        for (var i = 0; i < this.playerList; i++) {
        console.log('i was '+i);
        this.totalPlayers[0].data.push(i);
        console.log(this.totalPlayers);
      }
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="buttonClicked()">
    Click Me
  </button>
</div>

10 months ago · Santiago Trujillo Report

0

Just use this to push: this.totalPlayers[0].data.push(i); since totalPlayers is an array and you want to add to it's first element that is an object jsfiddle

10 months ago · Santiago Trujillo Report
Answer question
Find remote jobs