我有一个生成随机扑克手的函数,此后,我想删除所有花色,只剩下数字到数组中。最简单的方法是什么?我尝试了不同的方法,但收效不佳。
例如数组可能是这样的:
test = ["5♥","7♦","6♠","9♥","10♣"];
但我希望它像这样:
test = ["5","7","6","9","10"];
我想强调的是,在这种情况下,我不知道函数生成数组并删除花色时将是哪种数组。
谢谢您的帮助!
答案 0 :(得分:5)
答案 1 :(得分:3)
您可以删除每个字符串的最后一个字符。
let test = ["5♥","7♦","6♠","9♥","10♣"];
let out = test.map(a => a.slice(0, -1));
console.log(out);
答案 2 :(得分:1)
您可以使用regex
let test = ["5♥","7♦","6♠","9♥","10♣"];
console.log(test.map(str => str.replace(/.$/, '')));