简体   繁体   中英

How can I remove a “0” value from an event array javascript?

hello I have an array with the following structure

let test = ["testOne:,O,U,0","testTwo:R,C,0","testTree:1.334","testFour:r,z"];

I want to go through the array and eliminate the zero character "0" after the "," since I can have number values in some registers ", I have tried pop and slice but I only manage to eliminate by index from the array I want to eliminate those characters from the string in a new array

my expected result would be:

let test = ["testOne:,O,U","testTwo:R,C","testTree:1.334","testFour:r,z"];

Use endsWith method to check and slice the str. (Assuming always endsWith exactly ,0 , if they are different like , 0 , this needs to change accordingly)

 const arr = [ "testOne:,O,U,0", "testTwo:R,C,0", "testTree:1.334", "testFour:r,z", ]; const output = arr.map((str) => str.endsWith(",0")? str.slice(0, str.length - 2): str ); console.log(output);

You can use map and replace to remove ,0 from end of each string

 const arr = ["testOne:,O,U,0","testTwo:R,C,0","testTree:1.334","testFour:r,z"]; const res = arr.map(str => str.replace(/,0$/, "")); console.log(res);

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