How to set custom name in Javascript array index?

Asked

Viewed 1,096 times

3

In PHP we can use:

$validation = [
  200 => ['icon-check', 'The key match with the message!'],
  400 => ['icon-close', 'The key doesn\'t match with the message!'],
  403 => ['icon-close', 'Impossible to verify, the key or text can be wrong!']
];

This way we can obtain the result using $validation[200][0] to return icon-check.


However, I can’t do the same in Javascript:

var validation = [
   200 => ['icon-check', 'The key match with the message!'],
   400 : ['icon-close', 'The key doesn\'t match with the message!'],
   403 = ['icon-close', 'Impossible to verify, the key or text can be wrong!']
];

All kinds of attempts fail, both with =>, with : and with =.

My only alternative was to set the index in the variable:

var validation = [];

validation['200'] = ['icon-check', 'The key match with the message!'];
validation['400'] = ['icon-close', 'The key doesn\'t match with the message!'];
validation['403'] = ['icon-close', 'Impossible to verify, the key or text can be wrong!'];

However, is there any alternative without having to repeat the validation[XXX] = [...]?

  • I believe I can do something similar with object: var validation = {200: [...]}, but I don’t know if it’s the best approach.

  • http://answall.com/questions/41140

  • http://answall.com/questions/173916

2 answers

3


The correct(and only) way to have a string-index data structure is to create an object. Ex.:

var validation = {
    200: ['icon-check', 'The key match with the message!'],
    400: ['icon-close', 'The key doesn\'t match with the message!'].
    403: ['icon-close', 'Impossible to verify, the key or text can be wrong!']
}

The problem is due to the confusion of arrays concept that PHP provides.

You see, the PHP calls arrays what structurally is a ordered map, then it’s easy to think that $array = [ '400' => 'xxxxxx' ]; is an array when it is actually a Map<string, string>.

Javascript provides a data structure that is also a Map, are the Object, that can be created in literal form:

var obj = {
    400: "xxxxxxxx",
    "403": "yyyyyy"
}

Or with builders (not widely used):

var obj = new Object();
obj['400'] = "xxxx";
obj.propriedade = "yyyyy"

Anyway, every language has a string-indexed data structure because it is a very common and useful structure because the complexity of searching by index is usually O(1). In python we have dictionaries, in PHP the associative arrays, in java we have Hashmaps, etc..

0

To assign a key and value ALWAYS use json format instead of array:

var obj = {
    '200': ['icon-check', 'The key match with the message!'], 
    '400': ['icon-close', 'The key doesn\'t match with the message!']
}

But why do that? You ask me...

Well, you can even specify an index numerical for an array in javascript, however this is not recommended, as it will interpret the array as if it had values up to that particular index, for example:

var arr = [];
arr[200] = ['icon-check', 'The key match with the message!'];
console.log(arr.length);
// Result: 201

This way to have more flexibility when specifying a key and not run the risk of introducing a bug in your code, it is extremely recommended to use the format json.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.