2

I have a json array that has multiple missing numbers and is out of sequence. What is the best way to find which numbers are missing?

My First thought was to iterate through and construct a new temporary array in order (so if the first key is 50, it goes to arr[50]) and then find out which do not have a key. Unfortunately this seems extremely inefficient.

Update: Here's a bit of my json:

"groups": [
 {
 "group_id": "1",
 "group_name": "AABYODAADAAAW6KAAA",
 },
 {
 "group_id": "5",
 "group_name": "AABYODAADAAAW6KAAB",
 },
 {
 "group_id": "2",
 "group_name": "AABYODAADAAAW6KAAC",
 },
 {
 "group_id": "3",
 "group_name": "AABYODAADAAAW6KAAAD",
 },
 {
 "group_id": "6",
 "group_name": "AABYODAADAAAW6KAAAE",
 }
]

and I'm sorting group_id, but the array length is over 2,000.

Marcel Korpel
21.8k6 gold badges62 silver badges80 bronze badges
asked May 23, 2011 at 20:28
7
  • 1
    Is this a JS Array, a JS Object, a JSON Array, or a JSON Object? (Hint: JSON is a string.) Commented May 23, 2011 at 20:30
  • 4
    you may want to go accept some of your previous answers to get more help. Commented May 23, 2011 at 20:30
  • 1
    @Phrogz: A JS Array is still a JS Object. ;) SortingHat: Please read benalman.com/news/2010/03/theres-no-such-thing-as-a-json And do you have a bit of code to show us? Commented May 23, 2011 at 20:32
  • 1
    It can't be a ‘JSON Array’, ‘JSON’ and ‘Array’ are mutually exclusive. Please read the linked article. Commented May 23, 2011 at 20:49
  • 1
    You have superfluous commas at the end of each object within your array; that will cause lots of pain in several browsers. Commented May 24, 2011 at 13:46

2 Answers 2

6

Assuming this is a JS object you're talking about (and not a JS Array or a JSON Array or a JSON Object), you'll have to loop twice:

var max;
for (var key in obj) if (obj.hasOwnProperty(key) && (!max || key>max)) max = key;
for (var i=0;i<=max;++i) if (obj[i]==undefined){
 console.log("Missing: "+i);
}

Edit: Based on your updated sample, it appears that you have an array of objects whose keys are strings that represent integers, and you want to figure out keys might be missing. Here's code that would do that:

var groups = myObj.groups;
var groupNames = [];
for (var i=0,len=groups.length;i<len;++i){
 groupNames[groups[i].group_id] = groups[i].group_name;
}
for (i=0,len=groupNames.length;i<len;++i){
 var name = groupNames[i];
 if (name==undefined){
 console.log("Oops, no name for group_id: "+i);
 }else{
 // Do what you want
 }
}
answered May 23, 2011 at 20:31
2
  • @SortingHat I've updated my answer based on your sample code. Commented May 23, 2011 at 21:39
  • You don't have to declare i and len locally twice (the first var i... will suffice). Commented May 24, 2011 at 11:41
0

Perhaps you could construct a separate array with the numbers in the correct order, then iterate through the first array and have it remove ones from the secod array that match. What's leftin the newer array should be the missing numbers, in order.

answered May 23, 2011 at 20:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.