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