-1

From Array get only number into a new array or a string

var fetchMIDarray = [
  'Corp: nameofthecompany (345961663886)',
  'Chain: somerandomname (372395416889)'
]

except result for new array should be:

var fetchnumber =['345961663886','372395416889']

I know how this works for the string but unable to do same for array code sinppet for string.

var midname='Corp: Best Buy ApplePay/Partsearch (345961663886)';
var matches2 = midname.match(/\d+/);
console.log(matches2);
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
  • So... do that in a loop? – Heretic Monkey Jun 04 '21 at 15:19
  • `fetchMIDarray.flatMap(string => string.match(/\d+/))` returns what you seek. Note that `/\d+/` only finds the first number in a string. If there can be multiple numbers in a string use `/\d+/g` (`g` - global flag) instead. – 3limin4t0r Jun 04 '21 at 15:40

1 Answers1

2

You can use Array#map.

let arr = ['Corp: nameofthecompany (345961663886)', 'Chain: somerandomname (372395416889)'];
let res = arr.map(x => x.match(/\d+/)[0]);
console.log(res);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80