If my array is gave me:
["ITEM_1", "ITEM_2", "ITEM_3"]
How I can remove the part ITEM_
to get only ["1", "2", "3"]
?
If my array is gave me:
["ITEM_1", "ITEM_2", "ITEM_3"]
How I can remove the part ITEM_
to get only ["1", "2", "3"]
?
-
1
["ITEM_1", "ITEM_2", "ITEM_3"].map(e => e.replace('ITEM_', ''))
– Nenad Vracar Commented Apr 2, 2017 at 19:29 - You can use regex to extract number developer.mozilla/fr/docs/Web/JavaScript/Guide/… – Incognito Commented Apr 2, 2017 at 19:29
- @qatari, do you want to modify the initial array? – RomanPerekhrest Commented Apr 2, 2017 at 20:14
2 Answers
Reset to default 3or:
var arr = ["ITEM_1", "ITEM_2", "ITEM_3"].map(function(item){
return item.split('_')[1];
});
or:
var arr = ["ITEM_1", "ITEM_2", "ITEM_3"].map(function(item){
return item.replace('ITEM_', '');
});
You could do:
console.log(
["ITEM_1", "ITEM_2", "ITEM_3"].join().match(/\d+/g)
)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742296537a4417236.html
评论列表(0条)