JavaScript Multidimensional Arrays - Column for Row

Is it possible to turn a column of a multidimensional array into a string using JavaScript (possibly jQuery)? (without going through it)

so in the following example:

var data = new Array(); //data is a 2D array data.push([name1,id1,major1]); data.push([name2,id2,major2]); data.push([name3,id3,major3]); //etc.. 

Is it possible to get a list of identifiers from data without loops? thanks

+4
source share
4 answers

JavaScript does not contain multidimensional arrays. Which JavaScript allows you to have an array of arrays that you can interact with as if it were a multi-dimensional array.

As for your main question, no, you will need to go through the array to get a list of identifiers. This means that such an operation cannot be performed faster than the linear time O (n), where n is the height of the β€œ2D array”.

Also keep in mind that arrays in JavaScript are not necessarily represented in memory as continuous blocks. Therefore, any quick operations that may be familiar in other low-level languages ​​will not apply. The JavaScript programmer should treat arrays like Hash Tables , where the elements are just key / value pairs and the keys are indices (0, 1, 2 ...). You can still get / write elements in constant O (1) time (at least in modern JavaScript mechanisms), but often copy elements in O (n).

+3
source

No, it is impossible to build an array of identifiers without a loop.

In case you are interested, you will do this:

 var ids = []; for(var i = 0; i < data.length; i++) ids.push(data[i][1]); 

For better structural integrity, I would suggest using an array of objects, for example:

 data.push({"name": name1, "id": id1, "major":major1}); data.push({"name": name2, "id": id2, "major":major2}); data.push({"name": name3, "id": id3, "major":major3}); 

Then iterate over it like this:

 var ids = []; for(var i = 0; i < data.length; i++) ids.push(data[i].id); 
+10
source

You can use the array display function, which executes the loop for you:

 var ids = data.map(function(x) { return x[1] }); 

Unfortunately, like everything else on the Internet that would be really nice to use, the INTERNET EXPLORER DOES NOT SUPPORT THIS.

For more information about using the map function, see this page: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map

The good news is that the link above contains some good code in the Compatibility section, which checks for the existence of Array.prototype.map and determines if it is missing.

+1
source

You don’t need anything - create a line by combining it with newline characters and compare the middle of each line.

 var data1=[['Tom Swift','gf102387','Electronic Arts'], ['Bob White','ea3784567','Culinarey Arts'], ['Frank Open','bc87987','Janitorial Arts'], ['Sam Sneer','qw10214','Some Other Arts']]; data1.join('\n').match(/([^,]+)(?=,[^,]+\n)/g) /* returned value: (Array) gf102387,ea3784567,bc87987 */ 
+1
source

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


All Articles