Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
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>
over 4 years 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>

over 4 years 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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!