最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filtering 2d arrays using javascript - Stack Overflow

programmeradmin3浏览0评论

I have a 2D array where I need to filter the rows having date field (3d column)

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 prisons is time intensive, Is there a fastest way to retrieve?

I have a 2D array where I need to filter the rows having date field (3d column)

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 prisons is time intensive, Is there a fastest way to retrieve?

Share Improve this question asked Apr 13, 2019 at 14:29 Code GuyCode Guy 3,1985 gold badges42 silver badges95 bronze badges 10
  • a for loop ...? – Nina Scholz Commented Apr 13, 2019 at 14:29
  • Not by using traditional for loop. – Code Guy Commented Apr 13, 2019 at 14:30
  • I have 3000 rows to filter – Code Guy Commented Apr 13, 2019 at 14:30
  • 1 for loop will be fastest when it es to speed, and you should also add on what basis you're filtering data ? – Code Maniac Commented Apr 13, 2019 at 14:30
  • 1 @CodeGuy added an answer with simple for loop you can check – Code Maniac Commented Apr 13, 2019 at 14:38
 |  Show 5 more ments

5 Answers 5

Reset to default 7

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.

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

// You're using a Google Apps script, so this is ES5
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.

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

When it es 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

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.

Use Array#filter method with Array#some method.

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.

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)

发布评论

评论列表(0)

  1. 暂无评论