1

I want to sort an array of phone numbers and have the length of the array outputted based on areacode. For example:

var nums = [
    8881756223,
    8881742341,
    9187221757,
    ...,
]

there are a lot more entries than that (roughly 1300) and its already in numerical order. However, what I want it to do is:

  1. look at the first 3 numbers of the first entry
  2. look at the next entries first 3 numbers
  3. if they are different, then splice the array, console.log new array.length 
  and console.log that area code

so for example, the first two numbers in the array i provided will be spliced into their new array, and the console output will be:

areacode: 888, length: 1
areacode: 918, length: 0

I know the regex to search for the first the numbers, but I don't exactly know how to splice them into their own arrays...Like i know, use splice, but comparing the two with logic statements, I've never had to do something like that before while using a regular expression.

what I have so far is this:

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

var newArr = nums.filter(x => patt)

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

but this is spitting out the full number, not the area code its self. Of course ill be adding the logic to sort after i get it to just spit out area codes.

6
  • can you just give 15-20 rows of data so that i can test my solution Commented Aug 16, 2018 at 14:22
  • 2
    nums.map(x => ("" + x).replace(/^(\d{3})[^]*/, '$1')) Commented Aug 16, 2018 at 14:25
  • Probaly a duplicate of this: stackoverflow.com/questions/31731334/js-array-sort-with-regex Commented Aug 16, 2018 at 14:26
  • yeah. 6143617235, 6143928156, 7401156789, 7409012350, 7402055551, 7602791265, 7602323866, 7602567867, 7602921268, 8035982163, 8031346664, 8059136465, 8501299919, 8562190540, 8568451921, 8568672222, 8562345783, 9189281004, 9193570918, 9190678129, Commented Aug 16, 2018 at 14:31
  • @WiktorStribiżew this seems to have done it. Nice job, dang, i need to learn how to use regex.. Commented Aug 16, 2018 at 14:36

2 Answers 2

0

I suggest using

nums.map(x => ("" + x).replace(/^(\d{3})[^]*/, '$1'))

Here,

  • "" + x will coerce the number to string
  • .replace(/^(\d{3})[^]*/, '$1') will remove all chars keeping the first 3 digits (or the whole string upon no match).

JS Demo:

var nums = [
    8881756223,
    8881742341,
    9187221757,
    1
];
var res = nums.map(x => ("" + x).replace(/^(\d{3})[^]*/, '$1'));
console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

0

you can try this

var nums = [
    8881756223,
    8881742341,
    9187221757
]

var prefixes = nums.map(x=>(x+"").substr(0,3));

var votes =  prefixes.reduce(
(votes, curr) => {
    if(votes[curr]) votes[curr]++;
    else {votes[curr] =1;}
    return votes;
}, {});

var ans = Object.keys(votes).map(x => ({areacode:x, length:votes[x]}));

console.log(ans);

ans will hold the value you require

vote counting technique i used is explained here https://igghub.github.io/2017/01/15/useful-js-reduce-trick/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.