How to replace all occurrences of an array in JavaScript - Stack Overflow

If my array is gave me:["ITEM_1", "ITEM_2", "ITEM_3"]How I can remove t

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"] ?

Share edited Nov 8, 2020 at 14:43 Penny Liu 17.5k5 gold badges86 silver badges108 bronze badges asked Apr 2, 2017 at 19:27 F__MF__M 1,5883 gold badges21 silver badges34 bronze badges 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
Add a ment  | 

2 Answers 2

Reset to default 3

or:

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

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信