简体   繁体   中英

How to extract multiple columns from a two dimension array?

Could I have some guidance on how to extract multiple columns from a two-dimensional array like the below using JavaScript?

Array = [
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12],
];

the simple methods I've seen so far using forEach or map will allow extraction of ONE column at a time, is there a way to nest it somehow to extract whichever column index one desire?

Let's say the desire output column is 1,2 and 4.

Output = [
  [1, 2, 4],
  [5, 6, 8],
  [9,10,12],
];

EDIT:

Another problem I need to resolve is how to remove a row if it's Array[i][1]=0 or empty.

Let say we have extra array elements...

Array = [
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12],
  [13,0,15,16],
  [17, ,19,20],
  [21,22,23,24],
]; 

The desired output is now...

Output = [
  [1, 2, 4],
  [5, 6, 8],
  [9,10,12],
  [21,22,24],
]; 

You can .map() each row in arr to a new array which you can obtain by using an inner .map() on an array of columns/indexes which you wish to obtain. The inner map will map each index to its associated value from the row, giving you the values at each column for each row.

See example below:

 const arr = [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12], ]; const cols = [1, 2, 4]; const res = arr.map(r => cols.map(i => r[i-1])); console.log(res);

Further details: As you mentioned the first map exposes each inner array inside of arr to the inner mapping function:

cols.map(i => r[i-1])

This mapping function will loop through the indexes defined inside of cols and transform them into a new value. For example, say you're r is the second array:

[5, 6, 7, 8]

Performing cols.map(...) will loop over each element (denoted by i in the callback function) in [1, 2, 4] . For each element, we "transform" it into a new element:

i = 1
r = [5, 6, 7, 8]
new value: r[1-1] = 5

Next iteration we look at the next value in cols :

i = 2
r = [5, 6, 7, 8]
new value: r[2-1] = 6

Lastly, we look at the final value in cols :

i = 4
r = [5, 6, 7, 8]
new value: r[4-1] = 8

So the mapping function produces a new array which transforms the values from cols [1, 2, 4] , into the values at those indexes in the current row to be [5, 6, 8] . This occurs for each inner array / row, producing the final result.


EDIT As per your edit, you can apply the same logic from above to get your arrays with only the columns you desire. Once you have done that you can use .filter() to keep only the rows which have a truthy value in the second column. Keeping all the arrays with truthy values in the second column will remove the arrays with non-truthy values in the second column ( falsy values) from your resulting array. 0 and undefined are both flasy values, so they are not kept.

See example below:

 const arr = [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12], [13,0,15,16], [17, ,19,20], [21,22,23,24], ] const cols = [1, 2, 4]; const col = 2; const res = arr.filter(r => r[col-1]).map(r => cols.map(i => r[i-1])); console.log(res);

If you have multiple columns you want to ignore, you can use .every() to ensure each (ie: every) column contains a truthy value. If they all do, then .every() will return true , keeping the column, otherwise, it will return false , removing the column:

 const arr = [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12], [13,0,15,16], [ ,17,19,20], [21,22,23,24], ] const cols = [1, 2, 4]; const col = [1, 2]; const res = arr.filter(r => col.every(c => r[c-1])).map(r => cols.map(i => r[i-1])); console.log(res);

You can approach it the below given way. The solution is using the index parameter available in filter method .

Array.filter(currElem,index,array)

Since array is 0-indexed ,so I created the array with index you want in the data as [0,1,3] . You need to pass the data array and index array to the function .

 var array = [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12], ]; var arrIndex = [0,1,3]; function extractData(arr,indexArr) { return arr.map(obj => { return obj.filter((ob,index) => { if(indexArr.includes(index)){return ob} }) }) } console.log(extractData(array,arrIndex));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM