1

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"]
1
  • 1
    Try const expected = imgss.map(item => item.split(' ')[0]); . Commented Aug 23, 2022 at 6:07

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

Comments

0

here is what I did :

const imgss = ["ecdysisInfo 1", "growthLength 2", "wormHeadSize 1234"]
    const answer = imgss.map(str=> str.replace(' ','').split('').filter(item=>!item.match(/[0-9]/i)).join(''));
    console.log(answer)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.