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

4
  • 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? Commented Feb 12, 2019 at 18:24
  • Sorry for the unclear explanation, yes exactly! Commented Feb 12, 2019 at 18:25
  • Please share the code you already have so far and we will help you with it. Commented Feb 12, 2019 at 18:26
  • Possible duplicate of Filtering a two dimensional array in apps script Commented Feb 13, 2019 at 4:44

5 Answers 5

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Google Apps Script doesn't support arrow functions
@Rubén, OK, see my edit I leave the arrow function in a comment just for general use out of Google Apps, Thanks.
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)

2 Comments

Google Apps Script doesn't support arrow functions
@Rubén interesting. Replaced the arrow function.
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)

4 Comments

Google Apps Script doesn't support arrow functions
I have removed the arrow function.
Thanks. Maybe you would like to take a look to Filtering a two dimensional array in apps script
Sure. The solution for you is a bit different because I thought you wanted to filter based on the first element which is the city.
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')

2 Comments

Google Apps Script doesn't support arrow functions
@Rubén thanks for the heads up, I didn't read the question fully it seems.
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)

3 Comments

Why all the extra levels of arrays? Why not just call filter on the original array>
cause i just copy paste his input
Google Apps Script doesn't support arrow functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.