[Vuejs]-How to add object to object array (Vue.js)

1๐Ÿ‘

โœ…

[1] Either you can change the way you are inserting item to the object

new Vue({
  el: '#app',
  
  data: {
    ESBegriffe: {
                          0:{"id":0,"typ": 1,"Kategorie": 1,"Begriff":"foo","Reihenfolge":0},
                          1:{"id":1,"typ": 1,"Kategorie": 2,"Begriff":"foo","Reihenfolge":1},
                          2:{"id":2,"typ": 2,"Kategorie": 1,"Begriff":"foo","Reihenfolge":0},
                          3:{"id":3,"typ": 2,"Kategorie": 2,"Begriff":"foo","Reihenfolge":0},
                          4:{"id":4,"typ": 2,"Kategorie": 1,"Begriff":"foo","Reihenfolge":1},
    }
  },
  
  methods: {
    addBegriff: function(){
        var object = {"id": 5,"typ": 2,"Kategorie": 1,"Begriff": "BegriffK12","Reihenfolge":1};
        
        // Push is an array prototype constructor, you can not use it with `ESBegriffe` which is an object 
        this.$set(this.ESBegriffe, 5, object); 
    },
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">

  <!-- // TO VIEW OBJECT CONTENT -->
  <div
    v-for="(row, i) in ESBegriffe"
    :key="i"
  >
    {{ row }}
  </div>

  <!-- YOUR BTN -->
  <i @click="addBegriff"> Add(+) </i>

</div>

[2] Or you can change the object to an array to make it work

new Vue({
  el: '#app',
  
  data: {
    ESBegriffe: [
                          {"id":0,"typ": 1,"Kategorie": 1,"Begriff":"foo","Reihenfolge":0},
                          {"id":1,"typ": 1,"Kategorie": 2,"Begriff":"foo","Reihenfolge":1},
                          {"id":2,"typ": 2,"Kategorie": 1,"Begriff":"foo","Reihenfolge":0},
                          {"id":3,"typ": 2,"Kategorie": 2,"Begriff":"foo","Reihenfolge":0},
                          {"id":4,"typ": 2,"Kategorie": 1,"Begriff":"foo","Reihenfolge":1},
    ]
  },
  
  methods: {
    addBegriff: function(){
        var object = {"id": 5,"typ": 2,"Kategorie": 1,"Begriff": "BegriffK12","Reihenfolge":1};
        this.ESBegriffe.push(object);    
    },
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">

  <!-- // TO VIEW OBJECT CONTENT -->
  <div
    v-for="(row, i) in ESBegriffe"
    :key="i"
  >
    {{ row }}
  </div>

  <!-- YOUR BTN -->
  <i @click="addBegriff"> Add(+) </i>

</div>
๐Ÿ‘คShivam Singh

0๐Ÿ‘

make ESBegriffe as array

  ESBegriffe: [
                {"id":0,"typ": 1,"Kategorie": 1,"Begriff":"foo","Reihenfolge":0},
                {"id":1,"typ": 1,"Kategorie": 2,"Begriff":"foo","Reihenfolge":1},
                {"id":2,"typ": 2,"Kategorie": 1,"Begriff":"foo","Reihenfolge":0},
                {"id":3,"typ": 2,"Kategorie": 2,"Begriff":"foo","Reihenfolge":0},
                {"id":4,"typ": 2,"Kategorie": 1,"Begriff":"foo","Reihenfolge":1},
    ]

then it works.

๐Ÿ‘คcheppanu

0๐Ÿ‘

just merge the new object, replace:

this.ESBegriffe.push(object);

for

this.ESBegriffe = { ...this.ESBegriffe, ...object }

Leave a comment