Jams
Jams

Reputation: 81

JS Filter an array of arrays

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

Upvotes: 5

Views: 4448

Answers (5)

larz
larz

Reputation: 5766

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)

Upvotes: 3

Shay Moshe
Shay Moshe

Reputation: 482

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

Upvotes: 6

vmf91
vmf91

Reputation: 1937

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)

Upvotes: 1

Aziz.G
Aziz.G

Reputation: 3721

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)

Upvotes: 0

Alexei Darmin
Alexei Darmin

Reputation: 2129

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

Upvotes: 0

Related Questions