Damon
Damon

Reputation: 4484

Vue.js: How can I update an array of objects?

I am trying to update an array of objects in Vue.js. When trying to update the values of my location I am struggling to update the objects within an array.

When I log the objects out, I get this:

console.log(this.location) // {…}

console.log(this.location.additionalProperties);  // [{…}, __ob__: Observer]

console.log(this.location.additionalProperties.integrations);  // undefined

My additionalProperties object looks like this:

"additionalProperties": [
    {
      "integrations": [
        {
           "foo": {
               "locationId": 123,
               "example": "bar",
               ...
           }
        }
      ]
    }
],

I am passing in my location as a props like this:

location: {
    type: Object,
    required: true,
    default: () => ({}),
},

I know I am getting the location passed in correctly. I believe it is a syntax issue I am struggling with. I am trying to set my foo object to be something like this:

this.location.additionalProperties.integrations.foo = {
   locationId: 456,
   example: "testing",
   somethingElse: "anotherValue"
}

Using the above, I'll get a version of cannot set foo of undefined. How can I update the object within the additionalProperties array?

Thank you for any suggestions!

Upvotes: 2

Views: 92

Answers (1)

user10706046
user10706046

Reputation:

additionalProperties is an array

"additionalProperties": [
    {
      "integrations": [
        {
           "foo": {
               "locationId": 123,
               "example": "bar",
               ...
           }
        }
      ]
    }
],

this.location.additionalProperties[0].integrations.foo = ...

Upvotes: 2

Related Questions