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.
-
1Is this a JS Array, a JS Object, a JSON Array, or a JSON Object? (Hint: JSON is a string.)Phrogz– Phrogz2011年05月23日 20:30:26 +00:00Commented May 23, 2011 at 20:30
-
4you may want to go accept some of your previous answers to get more help.wajiw– wajiw2011年05月23日 20:30:41 +00:00Commented 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?Marcel Korpel– Marcel Korpel2011年05月23日 20:32:29 +00:00Commented May 23, 2011 at 20:32
-
1It can't be a ‘JSON Array’, ‘JSON’ and ‘Array’ are mutually exclusive. Please read the linked article.Marcel Korpel– Marcel Korpel2011年05月23日 20:49:42 +00:00Commented May 23, 2011 at 20:49
-
1You have superfluous commas at the end of each object within your array; that will cause lots of pain in several browsers.Marcel Korpel– Marcel Korpel2011年05月24日 13:46:08 +00:00Commented May 24, 2011 at 13:46
2 Answers 2
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
}
}
-
@SortingHat I've updated my answer based on your sample code.Phrogz– Phrogz2011年05月23日 21:39:04 +00:00Commented May 23, 2011 at 21:39
-
You don't have to declare
i
andlen
locally twice (the firstvar i...
will suffice).Marcel Korpel– Marcel Korpel2011年05月24日 11:41:05 +00:00Commented May 24, 2011 at 11:41
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.