Reputation: 53
i have data string
generate-image-scanID-60
generate-image-scanID-4
how to retrieve last digit with separator - ? in javascript
ouput :
thanks
Upvotes: 0
Views: 42
Reputation: 2141
Here you can use regex also :
let arr = ["generate-image-scanID-60", "generate-image-scanID-40"];
let t = arr.map((x) => +x.match(/^.*-(\d+)/)[1]);
console.log(t);
Upvotes: 0
Reputation: 2665
You can split the string and get last element and parse int
const raw = ['generate-image-scanID-60', 'generate-image-scanID-4']
const result = raw.map(str => parseInt(str.split('-').at(-1), 10))
console.log(result)
Upvotes: 0
Reputation: 13506
We can combine String.split()
and Array.at()
to do it
let datas =['generate-image-scanID-60','generate-image-scanID-4']
const getData = v => v.split('-').at(-1)
datas.forEach(d => {
console.log(getData(d))
})
Upvotes: 1