JavaScript: Map-Like Array, Sorting and Adding
Solution 1
1) Yes, push
would work (as would unshift
or splice
).
2) Object keys are inherently unordered and as such they do not have a sort
function :-) You could add a function to your map to return the values in sorted order however:
myMap.sort = function sortMap(sortFunc) {
var results = [];
for (key in this) {
if (this.hasOwnProperty(key) && this[key] !== sortMap) {
results.push(this[key]);
}
}
return results.sort(sortFunc);
};
myMap.sort(function(a, b) { return a.length - b.length; });
Some notes:
- Don't use
Array
ornew Array
unless you need to (e. g.Array(12).join("-")
to create a string of 11 dashes). Instead use the array literal syntax[]
. It's clearer (and in most browsers actually faster). - When in doubt, open up MDN and the console in your browser and try it out.
Solution 2
How would I sort the map so that the keys are ordered by the number of items in their arrays?
Maps are not ordered — there is no difference between { 'cats': [1,4,5], 'dogs': [2,3,6] }
and { 'dogs': [2,3,6], 'cats': [1,4,5] }
.
As for the rest of your question — yes, that's all correct. But instead of writing this:
var myMap = {};
myMap["cats"] = new Array(1,4,5); //animals 1,4,5 are cats
myMap["dogs"] = new Array(2,3,6); //animals 2,3,6 are dogs
I'd recommend writing this:
var myMap = {
'cats': [1,4,5], //animals 1,4,5 are cats
'dogs': [2,3,6] //animals 2,3,6 are dogs
};
or perhaps this:
var animals = [ 'cats', 'dogs', 'dogs', 'cats', 'cats', 'dogs' ];
felix_xiao
Updated on June 04, 2022Comments
-
felix_xiao almost 2 years
I'm trying to create a map or dictionary style data structure in JavaScript. Each string key points to an array of int, and I'd like to do a few simple things with the arrays that the keys point to.
I have a bunch of animals, each with an ID (1,2,3,...). I want to put these in a map so I know that 1,4,5 are cats and that 2,3,6 are dogs.
Here's what I have for code to explain it better.
var myMap = {}; myMap["cats"] = new Array(1,4,5); //animals 1,4,5 are cats myMap["dogs"] = new Array(2,3,6); //animals 2,3,6 are dogs
1) How would I add something to an array? For example, if animal #7 is a cat, would the following code be correct?
myMap["cats"].push(7); //so that myMap["cats"] is now an array with [1,4,5,7]
2) How would I sort the map so that the keys are ordered by the number of items in their arrays? In this case, myMap["cats"] would be in front of myMap["dogs"] because the array for "cats" has more items than the array for "dogs". Would the following code be correct?
myMap.sort(function(a,b){return myMap[b].length - myMap[a].length});
If there's a much more efficient way to do this in JavaScript, please let me know. Thank you so much!
-
Marty Cortez about 11 yearsthere is a difference between the pair of objects you first mentioned. jsfiddle.net/martco/dvdHq/1
-
Sean Vieira about 11 years@MartinCortez - yes, but that's because they are different objects not because their keys are in different source order ;-) jsfiddle.net/dvdHq/2
-
Marty Cortez about 11 years@SeanVieira i'm not questioning why they're different. i'm just pointing out that it's wrong to say that there is no difference.
-
ruakh about 11 years@MartinCortez: That makes no sense. You must realize that
{} === {}
also evaluates tofalse
(because each occurrence of{}
instantiates a new object); would you therefore insist that there is a difference between{}
and{}
? -
Marty Cortez about 11 yearsi'm not insisting anything besides that it's wrong to say there is no difference, as you did above
-
ruakh about 11 years@MartinCortez: Right, so you would object to the claim that "there is no difference between
{}
and{}
". As far as I'm concerned, that is insane. -
Marty Cortez about 11 years@ruakh as far as the compiler is concerned, that is
FALSE
. and the compiler doesn't care for your concerns -
ruakh about 11 years@MartinCortez: You do realize that, instead of digging in your heels and making increasingly insane assertions, you can just delete all your comments here and forget this ever happened?
-
Marty Cortez about 11 years@ruakh i feel more of a sense of light-hearted wonder, then heels dug-in. i don't care about remembering this, i only care about the unfortunate readers of your mis-information above.
-
ruakh about 11 years@MartinCortez: Don't worry, I'm quite certain that 6 billion readers will understand my answer correctly, and come away with correct information. (This includes the non-English-speakers and non-programmers.) You're certainly the only human with the particular problem you describe.
-
Marty Cortez about 11 years@ruakh in this case the only number that matters is the big fat zero on your answer. micdrop
-
ruakh about 11 years@MartinCortez: That's because you downvoted it. Also, you see the first comment to point out your craziness? It has three upvotes.