0

how can you change the value of an array 'reactively' by using Vue.js

data:{
cafe:[
{dish:’chips’},
{dish:’smokies’},
{dish:’hotdogs’},
(juice:’mango’)
]
}

suppose you want to change "chips" to "fish"

1
  • Then you can do this.cafe[0].dish = 'fish'; Commented Feb 12, 2020 at 11:56

2 Answers 2

1

Use $set for setting value.

this.$set(this.cafe[0],'dish','fish');

Codepen - https://codepen.io/Pratik__007/pen/GRJpvZp

Sign up to request clarification or add additional context in comments.

Comments

0

You could change your data structure a little bit, to make it more accessable.

data:{
  cafe: {
    dish: ["chips", "smokies", "hotdogs"],
    juice: ["mango"]
  }
 }

Then, you can filter a little bit easier.

this.cafe.dish = [...this.cafe.dish.filter(value => value !== "chips"), "fish"]

The dish array get's filtered and returns all values except chips, then you create a new array from the filter return and put in the fish.

Comments

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.