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

Slice and array after desired string

I have an array in which I have strings, in this I want to slice the array after I get "test?", but my logic of slicing isn’t working.

let input = ['hello', 'choose', 'test1', 'test?', 'yes,', 'no'];
let output = input.slice(input.indexOf('?') + 1, input.length);
console.log(output);

Desired Output:

["test?","yes"]

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 need to find the index of the array element where ? is there, then we can slice it, also this will find the first occurrence only!

let input = ['hello', 'choose', 'test1', 'test?', 'yes,', 'no'];
let output = input.slice(input.findIndex(x => x.includes('?')) + 1, input.length);
console.log(output);
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