简体   繁体   English

使用javascript过滤二维数组

[英]filtering 2d arrays using javascript

I have a 2D array where I need to filter the rows having date field (3d column)我有一个二维数组,我需要在其中过滤具有日期字段(3d 列)的行

var data = [
['1','a','12-12-2019','A'],
['2','b','','A'],
['3','c','12-1-2019','A'],
['4','d','','A'],
];

The expected result is预期的结果是

result = [
['1','a','12-12-2019','A'],
['3','c','12-1-2019','A'],
];

Using for loop for comprisons is time intensive, Is there a fastest way to retrieve?使用 for 循环进行 comprisons 是时间密集型的,有没有最快的检索方法?

I wouldn't worry about using a loop to do that - this is what loops are for.我不会担心使用循环来做到这一点 - 这就是循环的用途。

You could just use Array.prototype.filter() to make sure that there's a value in the 2nd position of each array, returning whether it's truthy or not.您可以只使用Array.prototype.filter()来确保每个数组的第二个位置都有一个值,返回它是否为真。

var data = [
  ['1','a','12-12-2019','A'],
  ['2','b','','A'],
  ['3','c','12-1-2019','A'],
  ['4','d','','A'],
];

// Used a classic `function` keyword because you're using this in a Google apps script
const result = data.filter(function(item) {
  return item[2];
});

console.log(result);

It seems pretty straightforward to just use Array.filter, checking to see if the date field is empty or not.只使用 Array.filter 似乎很简单,检查日期字段是否为空。

 var data = [ ['1','a','12-12-2019','A'], ['2','b','','A'], ['3','c','12-1-2019','A'], ['4','d','','A'], ]; var filtered = data.filter(e => e[2]); console.log(filtered);

Use Array#filter method with Array#some method.Array#filter方法与Array#some方法一起使用。

 var data = [ ['1', 'a', '12-12-2019', 'A'], ['2', 'b', '', 'A'], ['3', 'c', '12-1-2019', 'A'], ['4', 'd', '', 'A'], ]; // check any of the string matched date pattern let res = data.filter(arr => arr.some(str => /^\\d{1,2}-\\d{1,2}-\\d{4}$/.test(str))) console.log(res)

For the older browser, you can just do it with Array#filter method.对于较旧的浏览器,您只需使用Array#filter方法即可。

 var data = [ ['1', 'a', '12-12-2019', 'A'], ['2', 'b', '', 'A'], ['3', 'c', '12-1-2019', 'A'], ['4', 'd', '', 'A'], ]; var res = data.filter(function(arr) { // check length of date formated elements return arr.filter(function(str) { return /^\\d{1,2}-\\d{1,2}-\\d{4}$/.test(str) }).length; }); console.log(res)


If you just want to check the 3rd element always then there isn't any need of the nested loop.如果您只想始终检查第三个元素,则不需要嵌套循环。

 var data = [ ['1', 'a', '12-12-2019', 'A'], ['2', 'b', '', 'A'], ['3', 'c', '12-1-2019', 'A'], ['4', 'd', '', 'A'], ]; var res = data.filter(function(arr) { // check 3rd value is correct date value return /^\\d{1,2}-\\d{1,2}-\\d{4}$/.test(arr[0]) // if value would be empty all other case then simply // return the value since empty values are falsy // return arr[2]; }); console.log(res)

When it comes to speed for loop is fastest as your value to match is always at fixed index so you can just directly check and value and push your data当谈到速度时, for循环是最快的,因为您要匹配的值始终处于固定索引,因此您可以直接检查和值并推送您的数据

 var data = [['1','a','12-12-2019','A'],['2','b','','A'],['3','c','12-1-2019','A'],['4','d','','A'],]; let op = [] for(let i=0; i<data.length; i++){ if(!data[i][2]){ op.push(data[i]) } } console.log(op)

 var data = [ ['1','a','12-12-2019','A'], ['2','b','','A'], ['3','c','12-1-2019','A'], ['4','d','','A'], ]; d = data.filter(i => i[2].match(/\\d\\d?\\-\\d\\d?\\-\\d{4}/)) console.log(d)

If you really care the time, then use c++, c, rust or anything, btw, JS is the fastest interpreted language.如果你真的很在意时间,那就用c++、c、rust什么的吧,顺便说一句,JS是最快的解释型语言。

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

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