JS筛选数组

时间:2019-02-12 18:21:22

标签: javascript google-apps-script

我正在尝试过滤数组数组。

我在S.O上寻找答案。但是我遇到的所有问题都不符合我的要求(对象数组或未嵌套或格式不相同的简单数组,等等)

enter image description here

将值存储在数组中后,它们将如下所示:

[[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]]

我希望能够过滤此数组数组,并使数据仅链接到一个社区,例如巴黎。

我该怎么做?

非常感谢您的帮助

5 个答案:

答案 0 :(得分:2)

您可以使用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);

答案 1 :(得分:1)

假设数组中的所有元素均为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)

答案 2 :(得分:1)

我已将您的数组转换为JavaScript数组。过滤器将像这样完成:

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)

答案 3 :(得分:0)

此功能过滤列表中lst的元素。它会比较每个列表的第一个元素(在您的示例中为城市名称),并且仅当它等于“巴黎”时才返回true。

这是innerLst的结构不变的隐式假设。如果将城市名称移到其他索引,则larz的解决方案会解决这个问题。

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

答案 4 :(得分:0)

<?php
$linuxCmd = "/usr/bin/node /home/user/apps/app.js > /var/log/scr_log.log 2>&1 &";
exec($linuxCmd);