[Vuejs]-Running some javascript code before rendering in a list

2👍

Before you bind data fetched from API to view-model, do the transformation you want, example:

this.roomsData.room_photos = response.data.room_photos.map(item => {
  let url_parts = item.url.split('/')
  return location.origin + '/' + url_parts[url_parts.length - 1]
})

1👍

You can reduce the response and generate a new array from the provided objects.

const images_obj = response.data.room_photos.reduce((images, photo) => {
  images[] = { url: photo.url.split('/').pop() }

  return images
}, [])

0👍

You can do something like this to get the image name.

let url = "/id/118/200/300.jpg";

let filename = url.split('/').pop().split('?')[0];

Reference link : https://www.encodedna.com/javascript/how-to-get-the-filename-from-url-in-javascript.htm

Leave a comment