0

I have an array of phone numbers, say:

var phoneNums = [
    8881749281,
    8881533241,
    9198421881,
    ...,
]

Its a much larger list than that (roughly 1,300 entries) but for simplicity, we will just use that.

The array is already in numerical order, but, I want to sort this array into many different arrays based on the area code.

So the logic would be something like this:

for loop
    if first 3 numbers of this entry != first 3 numbers of the next entry
         array.splice

I've just never had to do something like this using a regular expression before, so I dont really know where to begin.

I know the regular expression to get the first 3 numbers:

 const patt = new RegExp('^\\d{3}')

And doing something like this: var newArr = nums.filter(x => patt)

 for (var i = 0; i < newArr.length; i++)
     console.log(newArr[i])

Is just giving me a list of all the full numbers, which isn't necessarily wrong, but I want it to just be the first 3 numbers.

Ext-
  • 471
  • 3
  • 8
  • 16
  • `const patt = new RegExp('^\\d{3}')` or `const patt = /^\d{3}/` – Wiktor Stribiżew Aug 15 '18 at 19:54
  • @WiktorStribiżew - That doesn't actually solve the problem, and your proposed duplicate does not address the fact that a RegExp object cannot be used as a callback function for a filter. – Travis J Aug 15 '18 at 19:58
  • Once the question is edited, yes, perhaps, it will no longer be a dupe. `new RegExp('/^\d{3}/')` just can't work, it is a common typo. – Wiktor Stribiżew Aug 15 '18 at 20:03
  • yeah I actually just changed that in my code and no it did not fix it. i realized i had done this pretty much right after typing it lol. ill edit it because i do think my question is different. – Ext- Aug 15 '18 at 20:16

0 Answers0