Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

JavaScript - object property order

0 contributions
17 points

In JavaScript object properties are not ordered by default, but it is possible to work with ordered properties in following ways.

1. Map class example

Note: this approach has been introduced in ES2015.

// ONLINE-RUNNER:browser;

var map = new Map();

map.set('key 5', 'a');
map.set('key 4', 'b');
map.set('key 3', 'c');
map.set('key 2', 'd');
map.set('key 1', 'e');

map.delete('key 3');

console.log('map size: ' + map.size);
console.log('key 2 -> ' + map.get('key 2'));
console.log('key 3 -> ' + map.get('key 3'));
console.log('key 4 -> ' + map.get('key 4'));
console.log('key 6 -> ' + map.get('key 6'));

map.forEach(function(value, key) {
    console.log('key: ' + key + ', value: ' + value);
});

Output (with NodeJS):

map size: 4

key 2 -> d
key 3 -> undefined
key 4 -> b
key 6 -> undefined

key: key 5, value: a
key: key 4, value: b
key: key 2, value: d
key: key 1, value: e

2. Custom ordered map class example

// ONLINE-RUNNER:browser;

function OrderedMap() {
    var array = [ ];
    var map = { };

    this.size = function() {
        return array.length;
    };

    this.exists = function(key) {
        return map.hasOwnProperty(key);
    };

    this.set = function(key, value) {
        var item = map[key];
        if (item) {
            item.value = value;
        } else {
            array.push(map[key] = {
                key: key,
                value: value
            });
        }
    };

    this.get = function(key) {
        if (map.hasOwnProperty(key)) {
            var item = map[key];
            if (item) {
                return item.value;
            }
        }
        return undefined;
    };

    this.remove = function(key) {
        var item = map[key];
        if (item) {
            var index = array.indexOf(item);
            if(index != -1) {
                array.splice(index, 1);
            }
            delete map[key];
        }
    };

    this.iterate = function(iteration) {
        for (var i = 0; i < array.length; ++i) {
            var item = array[i];
            iteration(i, item.key, item.value);
        }
    };
}


var map = new OrderedMap();

map.set('key 5', 'a');
map.set('key 4', 'b');
map.set('key 3', 'c');
map.set('key 2', 'd');
map.set('key 1', 'e');

map.remove('key 3');

console.log('map size: ' + map.size());
console.log('key 2 -> ' + map.get('key 2'));
console.log('key 3 -> ' + map.get('key 3'));
console.log('key 4 -> ' + map.get('key 4'));
console.log('key 6 -> ' + map.get('key 6'));

map.iterate(function(index, key, value) {
    console.log('index: ' + index + ', key: ' + key + ', value: ' + value);
});

Output (with NodeJS):

map size: 4

key 2 -> d
key 3 -> undefined
key 4 -> b
key 6 -> undefined

index: 0, key: key 5, value: a
index: 1, key: key 4, value: b
index: 2, key: key 2, value: d
index: 3, key: key 1, value: e

References

  1. Map class - MDN docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more