skip to Main Content

I have the following relTotal array.

const relTotal = [[11111,15 / 05 / 2024,11:11hs,teste de a3,admin],
[12541,03 / 06 / 2024,11:28hs,teste de a3,admin],
[151515,22 / 03 / 2024,15:15hs,testes,testetes],
[151515,22 / 05 / 2024,15:15hs,testes,testetes]]

The Date field is formatted as "dd / MM / yyyy" with spaces because I need this format for my application. My question is how do I filter this array using only the month MM as a reference?

If I use includes and pass the value "03" as a parameter, it will show all the lines where it appears and that’s not what I need

I tried using the following code

let retFilter = relTotal.filter(month=> month.includes("03"));
console.log(retFilter);

but it’s not filtering the way I need

2

Answers


  1. You could do something like this (assuming the way I fixed your relTotal definition is correct):

    const relTotal = [[11111,'15 / 05 / 2024','11:11hs','teste de a3','admin'],
    [12541,'03 / 06 / 2024','11:28hs','teste de a3','admin'],
    [151515,'22 / 03 / 2024','15:15hs','testes','testetes'],
    [151515,'22 / 05 / 2024','15:15hs','testes','testetes']]
    
    console.log(
    relTotal.filter(el => el[1].split(' / ')[1] == '03')
    )
    Login or Signup to reply.
  2. Use String#includes():

    const relTotal = [[11111,'15 / 05 / 2024','11:11hs','teste de a3','admin'],
    [12541,'03 / 06 / 2024','11:28hs','teste de a3','admin'],
    [151515,'22 / 03 / 2024','15:15hs','testes','testetes'],
    [151515,'22 / 05 / 2024','15:15hs','testes','testetes']]
    
    console.log(
    relTotal.filter(el => el[1].includes('/ 03'))
    )
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search