0

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);
    },
3
  • When is save method called and what does this.selectedfruits and this.list contains Commented Oct 29, 2020 at 8:51
  • When I select from selectbox and submit form , save method is called. @deceze Commented Oct 29, 2020 at 8:52
  • And do you want [ { "ward": 1, "status": true }, { "ward": 2, "status": true} ] in this.list? Commented Oct 29, 2020 at 8:56

3 Answers 3

1

You can try this

save(){
  this.list = []
  this.selectedFruits.forEach(e => {
      this.list.push({ ward: e, status: true });
    });
    console.log(this.list)
}
Sign up to request clarification or add additional context in comments.

Comments

0

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));

Comments

0

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)

4 Comments

When I did const newData = [({ ward: this.selectedFruits, status: true })], I'm getting newData as undefined. @S.Visser
Why are there ( ) in there?
When I remove ( ) , i'm still getting undefined. @S.Visser
Please make a jsfiddle with an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.