0

I'm looking to filter an array by the month and year which found on position 3 of the row where the dates are dd/mm/yyy.

array generated from a google sheets:

[[1, 134, DIOGO, 03/12/2019, ggggg, 2], [2, 131, ALEISIO, 13/11/2019, ggg, 33], [3, 134, DIOGO, 25/11/2019, gggg, 2], [4, 134, DIOGO, 15/12/2019, tttttt, 2]]

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

The expected results should be:

[[2, 131, ALEISIO, 13/11/2019, ggg, 33], [3, 134, DIOGO, 25/11/2019, gggg, 2]]

2
  • 1
    Can you clarify if you are dealing with an array, or a string? Because the "array" generated you are providing is not syntatically correct. There are no quotes around the strings and dates. Commented Nov 29, 2019 at 15:16
  • item[3] is a complete "date". How is item[3] === month && item[3] === year supposed to work? Commented Nov 29, 2019 at 15:19

1 Answer 1

4

Here is the code that will get the required data:

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

var filteredData = data.filter(function(item) {
var items = item[3].split('/');
return items[1] === month &&
       items[2] === year &&
       item[1] == funcionarioId
});
Sign up to request clarification or add additional context in comments.

5 Comments

You are correct ,My bad. I meant to also filter by the user Id which is position 2 in the array. For examplo user = "134". Sorry about that! So my result would be: [3, 134, DIOGO, 25/11/2019, gggg, 2]
Answer modified accordingly :)
That's awesome. Thank you.
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
@Andreas, thank you for the suggestion and I'm in agreement. The code above breacks up the string date dd/mm/yyyy in to Three parts var items = item[3].split('/'); and that allows me to filter the month and year separately. In my real app I also added other components to be filter by like the item[1] == funcionarioId. You guys are super cool!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.