andrewhayward / dijkstra

A JavaScript implementation of Dijkstra's algorithm
MIT License
228 stars 75 forks source link

Different JS Structure #9

Closed Biuni closed 6 years ago

Biuni commented 6 years ago

Hi andrew, thanks for this code. There's a way to implement this structure:

var map = [
    {
        node: 'a',
        next: [
            {
                id: 'b',
                weigth: 3
            },
            {
                id: 'c',
                weigth: 1
            }
        ]
    }, {
        node: 'b',
        next: [
            {
                id: 'a',
                weigth: 2
            },
            {
                id: 'c',
                weigth: 1
            }
        ]
    }, {
        node: 'c',
        next: [
            {
                id: 'a',
                weigth: 4
            },
            {
                id: 'd',
                weigth: 2
            }
        ]
    }, {
        node: 'd',
        next: []
    }
]

instead of

var map = {
    a: {
        b: 3,
        c: 1
    },
    b: {
        a: 2,
        c: 1
    },
    c: {
        a: 4,
        b: 1
    },
    d: {}
}

I ask you this because i have some nodes with alphanumeric string (e.g. 145EMA3) and the JS notation not allow the number into object key.

andrewhayward commented 6 years ago

Hi @Biuni... if you have problematic node names, you can just quote them:

var map = {
  "145EMA3": {
    ...
  },
  ...
};

Should you need to reference these at any point, you can do so with bracket notation - map["145EMA3"].