Initialize an array whose indices are scattered over an integer range

How to initialize a string array (size <100 elements) in javascript, whose indices are scattered throughout the integer range, with data elements.

If I like it:

array1 = ["string1","string2","string3","string4"]; 

then I get an array of length 4 with indices from 0 to 3

But in my case, I want to keep my own indexes so that the array can be used as a high-performance hash table with whole rows.

I prefer to look for one initialization.

The elements of the array should be accessible as follows: array1[23454]

Update from comments

I am not allowed to initialize the array as a single operator, since a line with the initialization of a dynamically prepared string is added on the server side as follows: var array = <string from server here>

+4
source share
4 answers

To create an array with a given number of indices, you can use

 // Creates an array with 12 indexes var myArray = new Array(12); 

This is not needed in javascript because of how its array works. There is no upper bound for arrays. If you try to reference the index of an element in an array that does not exist, undefined returned, but no error is thrown

To create an array with perpendicular indices, you can use something like array['index'] = value , although this will force you to use several operators. Javascript does not have an array initializer that allows you to specify indexes and values ​​in one of the operators, although you can create a function that will do as such

 function indexArray(param) { var a = [], i; for (i=0; i<param.length; i+=1) { a[param[i].index] = param[i].value; } return a; } var myArray = indexArray([ { index: 123456, value : "bananas" }, { index: 12, value : "grapes" }, { index: 564, value : "monkeys" } ]); 
+2
source
 var array1 = [] array1[23454] = 2 

Just doing it should be good. There is no fixed array size for javascript in the way that there is for java.

If you really want to do this all in one expression, you can make such an object as follows:

 var object1 = { "23454":2, "123":1, "50":3 }; 

and then enter the numbers as follows:

 object1["23454"] //2 

I really do not recommend this. An array method is a cleaner way to do this, even if it takes multiple lines, since it does not require string conversion. I do not know how this is implemented in browsers to comment on the impact of performance.

Update

Since the requirement of 1 line is based on the transfer of something to the server, I would recommend passing the JSON object to the server in the form:

 "{"23454":2,"123":1,"50":3}" 

then this code will analyze it on an object:

 var object1 = JSON.parse(jsonstringfromserver); 

and if you like, you can always convert it to an array by listing properties using a for for loop:

 var array1 = [] for ( num in object1){ array1[num] = object1[num]; 

This is probably not necessary, since object1 [123] will already return 1. You only need this if you plan to perform operations with the array.

+2
source

You do not need to pre-determine the size of the array before assigning it. For instance:

 var _array = []; _array[0] = "foo"; _array[1000] = "bar"; // _array.length => 1001 _array[1] //undefined 

You do not need to initialize the appropriate number of array elements before assigning them.

Update

It has already been pointed out that you can use an object, not an array. However, if you want to use array methods, this is still possible. Let me give you an example:

 var obj = { 0: 15, 1: 10, 2: 5, length: 3 }; 

If the object contains the length property, it can be considered as an object similar to an array. Although you cannot call array methods directly from these objects, you can use array methods.

 Array.prototype.join.call( obj ); // 15,10,5 

In fact, using the ECMAScript 5 map function, you can easily convert the above object into an array.

 var _array = Array.prototype.map.call( obj, function( x ) { return x; } ); 

The display function does not exist in all browsers, but you can use the following function if it is not.

 Array.map = Array.map || function(a, f, thisArg) { return Array.prototype.map.call(a, f, thisArg); } 
+1
source

You can do what you want with the object this way:

 var o = {23454: 'aaaa', 23473: 'bbb'}; 

You will lose the methods / fields of the array, for example. but you get what you were looking for, and you can easily add / remove members.

0
source

Source: https://habr.com/ru/post/1442112/


All Articles