prabina sht
prabina sht

Reputation: 217

Unable to copy an array to an array using vue.js

I have a multiple selectbox using vuetify select component. I'm getting array of selected items and I want to merge status as true of selected items like [ { "ward": 1, "status": true }, { "ward": 2, "status": true} ] I'm trying to copy selected items of array to another array but couldn't succeed. In console, I got selectedFruits as below.

enter image description here

enter image description here

 methods: {
    save() {
      console.log(this.selectedFruits);
      debugger;    
      this.selectedIndex.push({ ward: this.selectedFruits, status: true });
      console.log(this.list);
    },

Upvotes: 0

Views: 181

Answers (3)

beingyogi
beingyogi

Reputation: 1416

You can try this

save(){
  this.list = []
  this.selectedFruits.forEach(e => {
      this.list.push({ ward: e, status: true });
    });
    console.log(this.list)
}

Upvotes: 1

S.Visser
S.Visser

Reputation: 4725

You can use the Spread-Operator for this.

Here an example:

const existingArr = [{ hi: 'foobar' }];
const arr = [{ a: 'foo', b: 'bar'}, { a: 'hello',  b: 'world'}]
const newArr = [...existingArr, ...arr]

console.log(newArr)

Upvotes: 0

Ilya Degtyarenko
Ilya Degtyarenko

Reputation: 1569

If you want to copy an array or object without passing it by reference, do it like this

const newArray = JSON.parse(JSON.stringify(oldArray));

Upvotes: 0

Related Questions