0

New to Vuejs, have a question on updating an array.

This is my data (status_arr):

{
    "Approvals": [
        {
            "name": "Section 1",
            "count": 10692
        }
    ],
    "Declines": [
        {
            "name": "Section 1",
            "count": 1212
        },
        {
            "name": "Section 2",
            "count": 5
        }
    ]
}

I need to add a "Pending" part of the above data dynamically. I'm not quite understanding the $set method on how to accomplish this. I need to add to the above dataset without effecting the rest of the data in it.

The documentation says to use:

this.$set(status_arr, itemIndex, [ { 'name': 'Section 1', 'count': 0 } ]);

Not sure how to add the Pending part with the data attached and if I'm adding to the array, how does it have an itemIndex for me to do so? Is there a this.$add function or something that works like a pop()?

Thanks for any help!

1
  • What should the status_arr object look like after adding the Pending part? Commented Sep 26, 2017 at 0:10

3 Answers 3

2

If I'm understanding your question correctly, you want

this.$set(this.status_arr, 'Pending', [{
  name: 'Section 1',
  count: 0
}])
Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I wanted. Thanks!
@Sean you're welcome. See the other answer for an alternative that is less likely to trigger errors if you try to access status_arr.Pending before you've populated it
1

You can change status_array to this:

{
    "Approvals": [
        {
            "name": "Section 1",
            "count": 10692
        }
    ],
    "Declines": [
        {
            "name": "Section 1",
            "count": 1212
        },
        {
            "name": "Section 2",
            "count": 5
        }
    ],
    "Pending": []
}

And when you want to add more item to Pending, you can use:

status_array.Pending.push(newEle)

Remember, Vue wraps an observed array’s mutation methods so they will also trigger view updates. The wrapped methods are:

  • push()
  • pop()
  • shift()
  • unshift()
  • splice()
  • sort()
  • reverse()

Comments

0

Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it’s possible to add reactive properties to a nested object using the Vue.set or this.$set. This is how I achieve it:
A constructor function to return desirable object

 addObj(name, count) {
     return {
         name: name,
         count: count
     }
 }

Call this function

var newObj = new this.addObj('tom', 6);
this.$set(this.status_arr, 'Pending', newObj);

And if you want to remove a key val pair:

this.$delete(this.status_arr, 'Pending');

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.