Home > Back-end >  How to delete the last two characters of a number if it contains a slash (vue.js)
How to delete the last two characters of a number if it contains a slash (vue.js)

Time:12-13

I have a little problem with my code:

<template slot="popover">
  <img :src="'img/articles/'   item.id   '_1.jpg'">
</template>

Some of my item.id numbers (Exsample: 002917/1) have a slash in them. As a result, some images are not displayed. Now I would like to delet the last two charakters when there is an slash in the number. Is there a simple solution for this?

The slash should only be deleted at this point in the code and not in another place where the item.id is used.

I am very new to vue and javascript so please have mercy.

I am using the vue version 16.13.1

CodePudding user response:

You can simply use slice here as:

const num = "002917/1";
const result = num.includes("/") 
                ? num.slice(0, -2) 
                : num;
console.log(result);

CodePudding user response:

You could also use split. This will always give the correct result, even if there is no slash contained in the id.

<template slot="popover">
  <img :src="'img/articles/'   item.id.split('/')[0]   '_1.jpg'">
</template>
  • Related