Java Script 2 D Array Distribution

I want to allocate 2 D-array in Java Script.

I want to do something similar to this in JavaScript

typedef struct { int itemA, int itemB, string testC } myStruc; myStruct MyArray [100]; 

How to do it?

+4
source share
6 answers
 var arr = [] arr[0] = { "itemA": "A", "itemB": "B", "itemC": "C" } arr[1] = { "itemA": "A", "itemB": "B", "itemC": "C" } 

I think you are trying to apply static language constructs to the dynamic and different world of Javascript. Javascript really has no concept of arrays in the sense that many languages ​​do.

In Javascript, an array simply represents a special type of object (a hash itself) that has a property of special length. The integer "indexes" that you see above (that is, 0 in arr [0]) just have a search. A property of special length is defined as one larger than the largest integer key. In my example above, arr.length is 2. But if I were to assign:

 arr[100] = { "itemA": "A", "itemB": "B", "itemC": "C" } 

Then arr.length will be 101, although I did nothing to assign any of the elements from 2 to 99.

Similarly, we usually don’t predefine objects such as structures in Javascript, and thus anonymous objects will pretty much do what you want (or use a documented factory function, for example, in Ken's example).

"new Array ()" is optional as a short "var a = [];" the syntax is faster. :)

+6
source

If I understand, you need to dynamically create an array of a given length, filled with your own structure.

In js, a structure is an object [object Object] such as

  // typedef struct {type prop1, ..., type propN} myStructName;
     function myStructName () {}

     myStructName.prototype = {
         'constructor': myStructname,
         'prop1': defaultVal1,
         // ...
         'propN': defaultValN
     };

We can create a function that dynamically creates a structure type, but not in this case. Now we just need to create a function that creates an array of a certain length, filled with all instances of myStruct. This is my way

  / *
     Copyrights (c) 2011 - Matteo Giordani < matteo.giordani90@gmail.com >
     MIT-Style License
     typedef struct {int itemA, int itemB, string testC} myStruct
     myStruct arr [10];

  JS way function myStruct( a , b , c ) @param {int} a @param {int} b @param {string} c @return {object} 

* / function myStruct () {

// arguments var a = arguments[0] , b = arguments[1] , c = arguments[2]; // check INT type for argument a if( typeof a == "number" && (a + "").indexOf('.') == -1 ){ this['itemA'] = a; } // check INT type for argument b if( typeof b == "number" && (b + "").indexOf('.') == -1 ){ this['itemB'] = b; } // check INT type for argument b if( typeof c == "string" /*check for string length?!*/){ this['testC'] = c; } } // myStruct prototype myStruct.prototype = { // constructor 'constructor' : myStruct, // default value for itemA 'itemA' : 0, // default value for itemB 'itemB' : 0, // default value for testC 'testC' : '' }; /* static function defaultLength([, length]) Set/Get the defaultLength value. @param {unsigned int|void} length @return {void|unsigned int} */ myStruct.defaultLength = function(){ // return the default value if( arguments.length == 0 ){ return myStruct._default; }else{ // set the default value var l = arguments[0]; myStruct._default = ( typeof l == "number" && (l + "").indexOf('.') == -1 ) ? Math.abs( l ) : 0; } }; // @var {unsigned int} myStruct._default = 0 myStruct._default = 0; /* static function makeArray( length ) @param {unsigned int} length the length of the array @return {array} */ myStruct.makeArray = function( length ){ // Check if length is unsigned int length = ( typeof length == "number" && (length + "").indexOf('.') == -1 ) ? Math.abs( length ) : myStruct.defaultLength(); // local array var array = [] , i = 0; // populate the array for( ; i < length; i++){ array[ i ] = new myStruct(); } // return return array; }; // MAKE IT! myStruct.defaultLength(10); // set the default length == 10 var arr = myStruct.makeArray(); // [myStruct, myStruct, myStruct, myStruct, myStruct, myStruct, myStruct, myStruct, myStruct, myStruct] arr.length; // 10 Object.prototype.toString.call( arr ); // [object Array] /* ANOTHER EXAMPLE */ var arr2 = []; // make an empty array arr2[0] = new myStruct(1,1,'test1'); // make a first myStruct object arr2[1] = new myStruct(2,2,'test2'); // make a second myStruct object
+3
source

It sounds like you want to have properties for an array of objects, but I could be wrong.

You might need something like ...

 function Person(first, last) { this.first = first; this.last = last; } var person = new Person("John", "Dough"); 

http://www.kevlindev.com/tutorials/javascript/inheritance/index.htm

+1
source
 function Sample(value1, value2) { this.value1 = value1; this.value2 = value2; } var test = new Array(); test[0] = new Sample("a","aa"); test[1] = new Sample("b","bb"); 

PS: There are several ways to do this in Java Script.

+1
source

If you really want to allocate 100 elements of an array of a specific structure, you can do the following:

 arr = []; for (i=0; i<100; i++) { arr[i] = {itemA: <value>, itemB: <value>, textC: <string>, ... }; } 
+1
source

There are several ways to create arrays in Javascript, the method closest to what you want to do should be:

 var myArray = new Array("someString1", "someString2", "someString3", ...); 

You can replace "someString1" with real objects.

You can also use the Array constructor to set the initial length:

 var myArray = new Array(5); myArray[0] = ...; myArray[1] = ...; 

Arrays in Javascript are actually quite free - you don’t even need to determine the length before you access an element that usually goes beyond the array.

Check out the Mozilla documentation for arrays .

0
source

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


All Articles