How to get specific word from string array ?
let array= [
'age 19 - Donna',
'age 20 - James'
];
let getNumber = [] //19 and 20
let getName= [] // Donna and James
for(let i = 0; i < array.length; i++){
getNumber[i] = text[i].split(" ", 2)[1];
getName[i] = text[i].split(",", 5);
}
document.getElementById("demo").innerHTML = getNumber;
document.getElementById("demo2").innerHTML = getName;
current result:
19,20
age 19 – Donna,age 20 – James
desired result:
19,20
Donna,James
2
Answers
split
return array, so catch your desire result based on index of that array.Example:
Using
match
with a a regular expression might be a little simpler.Additional documentation