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

140
Views
How to update dynamic variable in array after change VUE

I want to assign a constant in an array to a variable and after changing this variable, the constant is updated with the new value of the variable.
But I cant understand what I must computed or watch.
Its example what I want
PageA.vue

 <div id="page-a">
      <div  class="card" 
            v-for="door in doors"
            :key="door.id">
    
        <div class="modelName">{{door.model}}</div>
      </div>
    </div>

    <script>
      export default {
        data() {
          return {
            firstDoorModel:"018",
            secondDoorModel:"015",
            doors: [    
             {model:firstDoorModel,
              id:"1"},
             {model:secondDoorModel,
              id:"2"},
             {model:firstDoorModel
              id:"3"}]
          }
        },
        methods: {
          changeModel(){
            this.firstDoorModel="012"
            this.secondDoorModel="010"
          }
        }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If I understood you correctly, try like following:

new Vue({
  el: "#page-a",
  data()   {
    return{
      doors: [{model: "", id: "1"}, {model: "", id: "2"}]
    }
  },
  methods: {
    changeModel(arr){
      for (let i = 0; i < this.doors.length; i++) {
        this.doors[i].model = arr[i]
      }
    }
  },
  mounted() {
    this.changeModel(["018", "015"])
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="page-a">
  <div class="card" 
       v-for="door in doors"
       :key="door.id"
  >
    <div class="modelName">{{ door.model }}</div>
  </div>
  <button @click="changeModel(['012', '010'])">Change</button>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Working Demo :

const app = new Vue({
  el: '#app',
  data() {
    return {
      doors: [{
        model: '018',
        id: '1'
      }, {
        model: '015',
        id: '2'
      }]
    }
  },
  methods: {
    changeModel(inputModelArr) {
        if (inputModelArr.length !== this.doors.length) {
       return;
      }
        this.doors.forEach((door, index) => {
        door.model = inputModelArr[index]
      });
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="door in doors" :key="door.id">
    {{ door.model }}
  </div><br>
  <button v-on:click="changeModel(['10', '20'])">Change Book Model</button>
</div>

about 4 years ago · Juan Pablo Isaza 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!