Gracie
Gracie

Reputation: 956

Convert Array into Single Object using Vue

How can I convert an array into a list of objects?

Turn

[
    {
        "currenttime":43481.46983805556,
        "moped":"30 minutes",
        "car":"1 hour"
    }
]

into

{
    "currenttime":43481.46983805556,
    "moped":"30 minutes",
    "car":"1 hour"
}

The data is retrieved from an external source and then the data edited using Vue.js

<script type="text/javascript">
    const app = new Vue({
    el: '#app',
    data: {   
        items: []
    },
    created: function() {
        fetch('https://example.com/delivery.json')
        .then(resp => resp.json())
        .then(items => {        
            this.items = items
        })
    }
    });
</script>

I had originally tried to output using an expression of {{ items.car }} and {{ items.0.car }} with no success

Upvotes: 0

Views: 4033

Answers (1)

Gracie
Gracie

Reputation: 956

Adding this, although not an ideal or correct solution does solve the issue for now.

this.items = items[0]

Upvotes: 1

Related Questions