最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Multidimensional arrays - column to row - Stack Overflow

programmeradmin0浏览0评论

Is it possible to turn a column of a multidimensional array to row using JavaScript (maybe 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

Is it possible to turn a column of a multidimensional array to row using JavaScript (maybe 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

Share Improve this question asked Dec 28, 2010 at 23:34 sarsnakesarsnake 27.7k62 gold badges185 silver badges293 bronze badges 1
  • Do you mean a comma seperated list when you say row? – Chandu Commented Dec 28, 2010 at 23:40
Add a comment  | 

4 Answers 4

Reset to default 11

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)

  1. 暂无评论