简体   繁体   English

JS过滤数组数组

[英]JS Filter an array of arrays

I'm trying to filter an array of arrays. 我正在尝试过滤数组数组。

I've searched for an answer on SO 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 ...) 我已经搜索过SO的答案,但是遇到的所有问题都与我的不匹配(对象数组或未嵌套或格式不相同的简单数组,等等...)

在此处输入图片说明

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 非常感谢你的帮助

you can use Array.filter, somthing like this: 您可以使用Array.filter这样的东西:

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

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 . 假设数组中的所有元素都是Strings ,则只需检查数组中的每个数组都包含元素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) 

I have converted your array to a JavaScript array. 我已经将您的数组转换为JavaScript数组。 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', 'NA', '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', 'NA', '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) 

This function filters the elements of lst which are lists. 此功能过滤列表的lst元素。 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'. 它比较每个列表的第一个元素(在您的示例中为城市名称),并且仅当它等于“巴黎”时才返回true。

This makes the implicit assumption that the structure of innerLst does not change. 这隐含了一个假设,即innerLst的结构不会改变。 If the city name is moved to a different index, then larz's solution accounts for that. 如果将城市名称移到其他索引,则larz的解决方案会解决这个问题。

lst.filter(innerLst => innerLst[0] === 'Paris')

 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, NA, 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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