How to define custom name in array index in Javascript?

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 get the result using $validation[200][0] to return icon-check .

However, I can not 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 types of attempts fail, both with => , : and = .

My only choice was to set the index on 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 an alternative without having to repeat validation[XXX] = [...] ?

    
asked by anonymous 27.01.2017 / 11:52

2 answers

3

The correct way (and the only one I know of) to have a data structure with string index 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 concept of arrays that PHP provides.

Well, PHP calls arrays what structurally is a ordered map , then it is easy to think that $array = [ '400' => 'xxxxxx' ]; is an array when in fact it is Map<string, string> .

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

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

Or with constructors (not much used):

var obj = new Object();
obj['400'] = "xxxx";
obj.propriedade = "yyyyy"
Finally, the whole language has a data structure indexed with strings because it is a very common and useful structure because the complexity of index search is usually O (1) . In python we have dictionaries , in PHP the associative associations , in Java we have HashMaps , etc.

    
27.01.2017 / 12:46
0

To assign a key and value ALWAYS use the 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 a numeric index for an array in javascript, however this is not recommended as it will interpret the array as having values up to that particular index, p>

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

In order to have more flexibility when specifying a key and not risk introducing a bug in your code, it is strongly recommended to use the json format.

    
27.01.2017 / 12:31