Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I delete a specific character from each string in an array in React native?

I have a string in an array called imgss . At this time, I want to subtract only the number from the string and put it in the answer variable. So imgss copied and then splice was written, but it does not work as there may be multiple numbers. How do I fix my code?

this is my code

    const imgss = ["ecdysisInfo 1", "growthLength 2", "wormHeadSize 1234"]


    const image = [...imgss]

    image.splice(0, 2);

    expected answer 

    const answer = ["ecdysisInfo", "growthLength", "wormHeadSize"]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can split each item with space

const imgss = ["ecdysisInfo 1", "growthLength 2", "wormHeadSize 1234"]

const image = imgss.map((item) => item.split(' ')[0]);

console.log(image)

OR you can use replace and regex to remove all numbers and spaces

const imgss = ["ecdysisInfo 1", "growthLength 2", "wormHeadSize 1234"]

const result = imgss.map((item) => item.replace(/[0-9 ]/g, ''));

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading