How to filter a multidimensional array using appscript

Viewed 33

This is my array look like

Sn...class... Name...roll... admission

  1. 1      X      2      111 
    
  2. 2      Y      8      147
    
  3. 3      Z      7      456
    
  4. 1      P      6      257
    
  5. 1      O      5      678
    
  6. 9      Y      3      789
    
  7. 4      I     19      679
    
  8. 5      H     14      101
    

Now I have to filter this data by class like how many student have in class 1 and then I only want to get the admission no. of all those students which belong to class 1. which is [111,257,678] in this case how can I do that beacsue when I use the filter method I am also getting the class like [[1,111],[1,257]] but I only want their admission no. without the class no.?

1 Answers

You might want to chain filter and map method calls like this.

const data = [
  [1, 1, 'X', 2, 111],
  [2, 2, 'Y', 8, 147],
  [3, 3, 'Z', 7, 456],
  [4, 1, 'P', 6, 257],
  [5, 1, 'O', 5, 678],
  [6, 9, 'Y', 3, 789],
  [7, 4, 'I', 19, 679],
  [8, 5, 'H', 14, 101]
]
const res = data
  .filter((item) => item[1] === 1)
  .map((item) => item[4])

console.log(res)

The resulting array is [ 111, 257, 678 ].

The answer greatly depends on the data structure. Anyway, I hope you understand the idea behind the method calls chaining to form the intended result.

Related