Filter an array of dates by the month’s equivalent number and the Javascript year

Asked

Viewed 149 times

1

Problem

I need to filter a string date array

example:

data = [[user1, 20/11/2019], [user1, 25/05/2019], [user1, 27/11/2019]]

Where the user selects the name the month "11" and the year "2019", in case the return would be:

"20/11/2019", "27/11/2019"

Code

var funcionarioId ="user1"
var month = "11"
var year = "2019";

var dataFiltered = data.filter(function(item){return item[1] === funcionarioId && 
 item[3] === month && 
 item[3] === year });

I believe I have to find the positions where the month and year are and apply the filter.

  • This and the structure copied directly from my console.log() [[1, 134, DIOGO, 03/12/2019, first, 2, 28/11/2019 09:43:23], [2, 131, ALEISIO, 13/11/2019, Second, 33, 28/11/2019 09:49:57], [3, 134, DIOGO, 25/11/2019, third, 2, 28/11/2019 09:43:23], [4, 134, DIOGO, 15/12/2019, Fourth, 2, 28/11/2019 09:43:23]]

1 answer

0

The code below resolves my situation. Thank you all for your patience.

var funcionarioId = '134';
var month = "11"
var year = "2019";

var filteredData = data.filter(function(item) {
var dataSplit = item[3].split('/');
return dataSplit[1] === month &&
       dataSplit[2] === year &&
       item[1] == funcionarioId
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.