简体   繁体   中英

How to remove characters from an array item?

I have a function which generate random poker hand and after that I want to delete all the suits and just leave numbers to the array. What is the easiest way to do that? I have tried different things without any good results.

For example array could be something like this:

test = ["5♥","7♦","6♠","9♥","10♣"];

but I want it to be like this:

test = ["5","7","6","9","10"];

I wanna emphasize that in this case I have no idea what kind of array it would be when the function generate the array and delete the suits.

Thank you for your help!

You can use map() and use slice() to remove the last element of string.

 const test = ["5♥","7♦","6♠","9♥","10♣"]; const res = test.map(x => x.slice(0,-1)); console.log(res) 

You could remove the last character of each string.

 let test = ["5♥","7♦","6♠","9♥","10♣"]; let out = test.map(a => a.slice(0, -1)); console.log(out); 

You can use regex ,

 let test = ["5♥","7♦","6♠","9♥","10♣"]; console.log(test.map(str => str.replace(/.$/, ''))); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM