vuejs2 - How should i go when adding an object to an array using javascript and vue? - Stack Overflow

Sorry if its a very easy question, i tried following some answers in here but i couldnt..I want to add

Sorry if its a very easy question, i tried following some answers in here but i couldnt..

I want to add a NEW object to an array, based on the first one

The way i find that works is this one:

    new Vue({
    el: "#app",
    data: {
       name: '',  //name isnt inside and object
     //failedExample: {name: ''}
        array: []
    },
    methods: {
        add(){
            this.array.push({name: this.name}) // i push a key:value
          //this.array.push(failedExample) // what i wished to do
        }
    }
});

/

I understand that by using the mented array.push, i would just be adding the same reference to the object over and over, so when i change the value of failedExample.name, it will change in all positions of the array. Is there a way that this doesnt happens? Like, i add the first object, then the next one as a NEW instead of a reference?

Sorry if its a very easy question, i tried following some answers in here but i couldnt..

I want to add a NEW object to an array, based on the first one

The way i find that works is this one:

    new Vue({
    el: "#app",
    data: {
       name: '',  //name isnt inside and object
     //failedExample: {name: ''}
        array: []
    },
    methods: {
        add(){
            this.array.push({name: this.name}) // i push a key:value
          //this.array.push(failedExample) // what i wished to do
        }
    }
});

https://jsfiddle/myrgato/6mvx0y1a/

I understand that by using the mented array.push, i would just be adding the same reference to the object over and over, so when i change the value of failedExample.name, it will change in all positions of the array. Is there a way that this doesnt happens? Like, i add the first object, then the next one as a NEW instead of a reference?

Share Improve this question asked Jul 20, 2017 at 15:03 Vinicius SouzaVinicius Souza 1433 silver badges11 bronze badges 1
  • Can you give us an example of your desired oute? – larz Commented Jul 20, 2017 at 15:11
Add a ment  | 

1 Answer 1

Reset to default 6

It should work as you wanted to do with your 'failedExample'. The only wrong thing I see is that you forget the this keyword when you're pushing into array.

So try this one:

 new Vue({
    el: "#app",
    data: {
      failedExample: { name: 'test'},
      array: []
    },
    methods: {
        add(){
          this.array.push(this.failedExample);
          console.log(this.array);
        }
    }
});

Update: If you want to add a new object each time, then try to clone it so you will not have refference problems:

this.array.push(Object.assign({}, this.failedExample));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744718425a4589764.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信