开发者

JavaScript Multidimensional arrays - column to row

开发者 https://www.devze.com 2023-02-01 06:48 出处:网络
Is it possible to turn a column of a multidimensional array to row using JavaScript (m开发者_如何学Caybe Jquery)? (without looping through it)

Is it possible to turn a column of a multidimensional array to row using JavaScript (m开发者_如何学Caybe Jquery)? (without looping through it)

so in the example below:

 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 possible to get a list of IDs from data without looping? thanks


No, it is not possible to construct an array of IDs without looping.

In case you were wondering, you'd do it like this:

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

For better structural integrity, I'd suggest using an array of objects, like so:

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 through it like so:

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


JavaScript doesn't really have multidimensional arrays. What JavaScript allows you to have is an array of arrays, with which you can interact as if it was a multidimensional array.

As for your main question, no, you would have to loop through the array to get the list of IDs. It means that such an operation cannot be done faster than in 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 contiguous blocks. Therefore any fast operations that you might be familiar with in other low level languages will not apply. The JavaScript programmer should treat arrays as Hash Tables, where the elements are simply key/value pairs, and the keys are the indices (0, 1, 2...). You can still access/write elements in constant time O(1) (at least in modern JavaScript engines), but copying of elements will often be done in O(n).


You could use the Array map function which does the looping for you:

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

Unfortunately, like everything else on the web that would be really nice to use, INTERNET EXPLORER DOESN'T SUPPORT IT.

See this page for details on how the map function works: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map

The good news it that the link above provides some nice code in the "Compatibility" section which will check for the existence of Array.prototype.map and define it if it's missing.


You don't need anything special- make a string by joining with newlines, and match 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
*/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号