5

I'm trying to filter an array of arrays.

I've searched for an answer on S.O. but all questions I've come accross don't match mine (array of objects or a simple array not nested or not the same format, etc ...)

enter image description here

Once the values are stored in an array they look like this:

[[Paris, One ONE, Boss, Wed Mar 01 00:00:00 GMT+01:00 2017, ], [Paris, Two TWO, Temp, Sat Jul 01 00:00:00 GMT+02:00 2017, ], [Paris, Three THREE, Employee, Sat Sep 01 00:00:00 GMT+02:00 2018, ], [Paris, Four FOUR, Intern, Thu Nov 01 00:00:00 GMT+01:00 2018, ], [Paris, Five FIVE, N.A., Sat Dec 01 00:00:00 GMT+01:00 2018, ], [Paris, Six SIX, Director, Tue Jan 01 00:00:00 GMT+01:00 2019, ], [Paris, Seven SEVEN, Director, Fri Jan 01 00:00:00 GMT+01:00 2016, Sun Jul 01 00:00:00 GMT+02:00 2018], [Paris, Eight EIGHT, Director, Fri Jan 01 00:00:00 GMT+01:00 2016, Sun Oct 01 00:00:00 GMT+02:00 2017], [Paris, Nine NINE, N.A., Thu Nov 01 00:00:00 GMT+01:00 2018, Sat Dec 01 00:00:00 GMT+01:00 2018], [London, Ten TEN, Employee, Fri Jan 01 00:00:00 GMT+01:00 2016, Mon Oct 01 00:00:00 GMT+02:00 2018], [London, Eleven ELEVEN, Intern, Mon Feb 01 00:00:00 GMT+01:00 2016, Mon Jan 01 00:00:00 GMT+01:00 2018], [London, Twelve TWELVE, Employee, Sun May 01 00:00:00 GMT+02:00 2016, Sun Oct 01 00:00:00 GMT+02:00 2017]]

I would like to be able to filter this array of arrays and keep the data linked to one community only, Paris for instance.

How would I do that?

Thanks a lot for your help

Taki
  • 17,320
  • 4
  • 26
  • 47
Jams
  • 81
  • 1
  • 8
  • When you say `keep the data linked to one community only` you mean you want to filter so the result has only the `Paris` rows? – Mark Feb 12 '19 at 18:24
  • Sorry for the unclear explanation, yes exactly! – Jams Feb 12 '19 at 18:25
  • Please share the code you already have so far and we will help you with it. – chrisg86 Feb 12 '19 at 18:26
  • Possible duplicate of [Filtering a two dimensional array in apps script](https://stackoverflow.com/questions/51004121/filtering-a-two-dimensional-array-in-apps-script) – Rubén Feb 13 '19 at 04:44

5 Answers5

6

you can use Array.filter, somthing like this:

const data = [
['Paris', 'One ONE', 'Boss', 'Wed Mar 01 00:00:00 GMT+01:00 2017' ],
['Paris', 'Two TWO', 'Temp', 'Sat Jul 01 00:00:00 GMT+02:00 2017' ],
['London', 'Three THREE', 'Employee, Sat Sep 01 00:00:00 GMT+02:00 2018' ]];

const result = data.filter(function(item) { return item[0]==='Paris'});
// const result = data.filter(item => item[0]==='Paris'); // ES6
console.log(result);
Shay Moshe
  • 482
  • 4
  • 16
3

Assuming all of the elements in your arrays are Strings, you just check that each array in your array of arrays contains the element Paris.

const yourArray = [
  ['Paris', 'one One', 'whatever else'],
  ['Paris', 'one One', 'whatever else'],
  ['Paris', 'one One', 'whatever else'],
  ['Paris', 'one One', 'whatever else'],
  ['London', 'one One', 'whatever else'],
]

const onlyParis = yourArray.filter(function(array) {
  return array.includes('Paris')
})

console.log(onlyParis)
larz
  • 5,724
  • 2
  • 11
  • 20
1

I have converted your array to a JavaScript array. And the filter will be done like this:

var myArray = [['Paris', 'One ONE', 'Boss', 'Wed Mar 01 00:00:00 GMT+01:00 2017'],['Paris', 'Two TWO', 'Temp', 'Sat Jul 01 00:00:00 GMT+02:00 2017'],['Paris', 'Three THREE', 'Employee', 'Sat Sep 01 00:00:00 GMT+02:00 2018'],['Paris', 'Four FOUR', 'Intern', 'Thu Nov 01 00:00:00 GMT+01:00 2018'],['Paris', 'Five FIVE', 'N.A.', 'Sat Dec 01 00:00:00 GMT+01:00 2018'],['Paris', 'Six SIX', 'Director', 'Tue Jan 01 00:00:00 GMT+01:00 2019'],['Paris', 'Seven SEVEN', 'Director', 'Fri Jan 01 00:00:00 GMT+01:00 2016', 'Sun Jul 01 00:00:00 GMT+02:00 2018'],['Paris', 'Eight EIGHT', 'Director', 'Fri Jan 01 00:00:00 GMT+01:00 2016', 'Sun Oct 01 00:00:00 GMT+02:00 2017'],['Paris', 'Nine NINE', 'N.A.', 'Thu Nov 01 00:00:00 GMT+01:00 2018', 'Sat Dec 01 00:00:00 GMT+01:00 2018'],['London', 'Ten TEN', 'Employee', 'Fri Jan 01 00:00:00 GMT+01:00 2016', 'Mon Oct 01 00:00:00 GMT+02:00 2018'],['London', 'Eleven ELEVEN', 'Intern', 'Mon Feb 01 00:00:00 GMT+01:00 2016', 'Mon Jan 01 00:00:00 GMT+01:00 2018'],['London', 'Twelve TWELVE', 'Employee', 'Sun May 01 00:00:00 GMT+02:00 2016', 'Sun Oct 01 00:00:00 GMT+02:00 2017']]

var filteredArray = myArray.filter(function (item){
 return item[0] == 'Paris'
})

console.log(filteredArray)
vmf91
  • 1,897
  • 19
  • 27
0

This function filters the elements of lst which are lists. It compares each list's first element (which is the city name in your example) and returns true if and only if it is equal to 'Paris'.

This makes the implicit assumption that the structure of innerLst does not change. If the city name is moved to a different index, then larz's solution accounts for that.

lst.filter(innerLst => innerLst[0] === 'Paris')
Alexei Darmin
  • 2,079
  • 1
  • 18
  • 30
0

const country = [
  ["Paris", "One ONE, Boss, Wed Mar 01 00: 00: 00 GMT + 01: 00 2017", ],
  ["Paris", "Two TWO, Temp, Sat Jul 01 00: 00: 00 GMT + 02: 00 2017", ],
  ["Paris", "Three THREE, Employee, Sat Sep 01 00: 00: 00 GMT + 02: 00 2018", ],
  ["Paris", "Four FOUR, Intern, Thu Nov 01 00: 00: 00 GMT + 01: 00 2018", ],
  ["Paris", "Five FIVE, N.A., Sat Dec 01 00: 00: 00 GMT + 01: 00 2018", ],
  ["Paris, Seven SEVEN, Director, Fri Jan 01 00: 00: 00 GMT + 01: 00 2016, Sun Jul 01 00: 00: 00 GMT + 02: 00 2018"],
  ["London", "Twelve TWELVE, Employee, Sun May 01 00: 00: 00 GMT + 02: 00 2016", "Sun Oct 01 00: 00: 00 GMT + 02: 00 2017"]
]

const res = country.filter(([ville, b, c]) => ville === "Paris")


console.log(res)
Aziz.G
  • 3,599
  • 2
  • 17
  • 35