0

I need to create object in javascript that allows to fetch values using keys & also iterate over keys. The primary requirement is fetching value by key but iteration is required to maintain sort order of entries by values(integer).

How do I go about creating such an object ?

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294
  • 1
    check this post here http://stackoverflow.com/questions/1078118/how-to-iterate-over-a-json-structure – ManMohan Vyas Sep 24 '12 at 05:32
  • @ManMohan Vyas: just iteration is ok, but how to retrieve value by key ? – Rajat Gupta Sep 24 '12 at 05:36
  • 1
    @user01: Retrieve value by key is easy: the most general way is obj['keyname'], a less general way is obj.keyname – nhahtdh Sep 24 '12 at 05:37
  • @nhahtdh: Thanks. Please also suggest a way to append entries to `sampleJson={ "1":"john", "2":"johny" }` to add `"3":"johnyoo"` – Rajat Gupta Sep 24 '12 at 05:53
  • 2
    @user01: You should get a good book on JS before even start coding. As for that sampleJson['3'] = "johnyoo". But the problem is, normal JS Object won't fulfill your second condition of "maintain sort order of entries by values(integer)" while allow fetching by key. – nhahtdh Sep 24 '12 at 05:56
  • @user01 http://stackoverflow.com/questions/6503193/nested-json-how-to-add-push-new-items-to-an-object check that – ManMohan Vyas Sep 24 '12 at 05:56

3 Answers3

1
  1. All objects in JavaScript are JSONeable! (is that really a word).
  2. All objects in JavaScript are a collection of key value mappings.
  3. A for in loop iterates over the keys of an object.
ColinE
  • 68,894
  • 15
  • 164
  • 232
  • 1
    Your answer does not answer the question: "maintain sort order of entries by values(integer)." – nhahtdh Sep 24 '12 at 05:31
1
sampleJson={
"1":"john",
"2":"johny"
}

You can iterate using for in loop

for(key in sampleJson){
//ur code
}
Apurv
  • 309
  • 2
  • 10
0

Native objects don't support exactly what you're looking for, but it's fairly straightforward to create a wrapper around native objects that provides extra functionality.

A possible approach:

function KeySortArr(obj) {
    this.obj = obj || {};
}

KeySortArr.prototype = {
    get: function(key) {
        return this.obj[key];
    },

    set: function(key, value) {
        this.obj[key] = value;
    },

    keys: function() {
        var keys = [];
        for(var i in this.obj) {
            if (this.obj.hasOwnProperty(i))
                keys.push(i);
        }
        return keys;
    },

    getKeysSortedByValue: function() {
        var obj = this.obj;
        return this.keys().sort(function(a, b) {
            return obj[a] > obj[b];
        });
    }
};

Usage:

var arr = new KeySortArr();
arr.set("test", 4);
arr.set("another", 2);
arr.set("ok", 60);
arr.set("last", 14);

The following then:

arr.getKeysSortedByValue();

Returns:

["another, "test", "last", "ok"]

In other words, the keys are sorted by their associated value. This might not be exactly what you were looking for, but it should be close.

Wayne
  • 59,728
  • 15
  • 131
  • 126