0

I am using VueJs and I have an array of objects. What i want to do is update an object in the array based on its index. For that I've written a method:

updateRow(index) {
      this.inputs[index]={ "one": "test", "two": "test" }
    }

However, the object just doesn't update. I've created the following jsbin for it.

https://jsbin.com/boturuluxe/1/edit?html,js,console,output

Any help is appreciated.

3 Answers 3

2

Due to limitations in JavaScript, Vue cannot detect the following changes to an array:

  1. When you directly set an item with the index, e.g. vm.items[indexOfItem] = newValue

  2. When you modify the length of the array, e.g. vm.items.length = newLength

The solution is: this.$set(this.inputs, index, { "one": "test", "two": "test" })

Read more here

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

Comments

0

Use this

updateRow(index) {
  Vue.set(this.inputs, index, { "one": "test", "two": "test" })
}

Comments

0

You can do:

updateRow(index) {
  this.inputs[index]={ "one": "test", "two": "test" };
  this.inputs = Object.assign({}, this.inputs);
  // assigning a new object reference
}

Or,

updateRow(index) {
  this.$set(this.inputs, index, { "one": "test", "two": "test" });
}

Vue docs reference

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.