[Vuejs]-VueJS Axios JSON slice/filter is not a function

2πŸ‘

βœ…

It looks like your data is an object of key/value pairs. Unfortunately, the majority of browsers don’t support Object.values which will give you an array of just the values of the object:

return Object.values(this.vehicles)    // Array of vehicle objects
  .filter(function (v) {
    return v.name === ('aaa' || 'bbb')
  })

…but there is an Object.keys method which is largely supported, so you can get to the values with a two-step process:

return Object.keys(this.vehicles)     // Array of keys in the JSON obj
  .map(key => this.vehicles[key])     // Array of vehicle objects
  .filter(function (v) {
    return v.name === ('aaa' || 'bbb')
  })

Or if you want to use something like lodash, that gives you some more convenient functions for dealing with manipulating objects.

πŸ‘€Jacob

0πŸ‘

put this in your app.js file

Vue.filter('snippet',function (val) {
    return val.slice(0,100);
})

and when you want to use it just put snippest in your html like this.

<p class="card-text mb-auto">{{product.description | snippet}}</p>

Leave a comment