Reputation: 1596
What I do in php:
$arr = [1=>'a', 3=>'b', 2017=>'zzzzZZZZ'];
What I konw in js:
var arr = [];
arr[1] = 'a';
arr[3] = 'b';
arr[2017] = 'zzzzZZZZ';
Upvotes: 2
Views: 264
Reputation: 115242
Your code would make an array of length 2018
since the largest array index defined is 2017
and the remaining undefined elements are treated as undefined
.
var arr = [];
arr[1] = 'a';
arr[3] = 'b';
arr[2017] = 'zzzzZZZZ';
console.log(arr.length, arr);
var obj = {
1 : 'a',
3 : 'b',
2017 : 'zzzzZZZZ'
}
var obj = {
1: 'a',
3: 'b',
2017: 'zzzzZZZZ'
}
console.log(obj);
Refer : javascript Associate array
Upvotes: 1