问题描述
你好,我有一个具有以下结构的数组
let test = ["testOne:,O,U,0","testTwo:R,C,"testTree:1.334","testFour:r,z"];
我想遍历数组并消除“”之后的零字符“ 0”,因为我可以在某些寄存器中具有数字值,所以我尝试了pop和slice,但是我只能设法通过索引来消除从数组中我想从新数组的字符串中消除那些字符
我的预期结果是:
let test = ["testOne:,U",C",z"];
解决方法
使用endsWith
方法检查和切片str。
(假设总以,0
结尾,如果它们与,0
不同,则需要相应地进行更改)
const arr = [
"testOne:,O,U,0","testTwo:R,C,"testTree:1.334","testFour:r,z",];
const output = arr.map((str) =>
str.endsWith(",0") ? str.slice(0,str.length - 2) : str
);
console.log(output);
,
您可以使用map
和replace
从每个字符串的末尾删除,0
const arr = ["testOne:,z"];
const res = arr.map(str => str.replace(/,0$/,""));
console.log(res);