0

I have the following array named parsedAutor and I need to remove the empty elements from the nested arrays.

[
  ['John Doe', '', 'CPF 000.000.000-00'],
  ['30/05/2018 - Vara de Delitos de Roubo e Extorsão'],
  ['John Doe', '', 'CPF 000.000.000-00'],
  ['29/02/2016 - 1ª Vara Criminal'],
  ['John Doe', '', 'CPF 000.000.000-00'],
  ['18/02/2016 - 3º Juizado Especial Cível'],
  ['John Doe', '', 'CPF 000.000.000-00'],
  ['18/02/2016 - 3º Juizado Especial Cível']
]

How do I manage to do it? I've been trying to map the elements and then filter them, but it's not working and I think I'm doing it wrong.

Here's what I've been trying to do.

const autor = $('div[class="espacamentoLinhas"]').toArray();

let parsedAutor = autor.map((x) => x.children[2].data.trim());

console.log(parsedAutor);

parsedAutor = parsedAutor.map((x) => x.split('\n').map((y) => y.trim()));

console.log(parsedAutor);

// note that the code above is just to get the context from where I taking the values,  please focus on the code below

const filteredAutor = parsedAutor.map((x) => {
  x.filter((y) => y !== '');
});

console.log(filteredAutor);

But it returns me eight undefined values, what am I doing wrong?

Thanks in advance.

Nick
  • 624
  • 7
  • 18
innis
  • 360
  • 3
  • 14

5 Answers5

1

Your code is almost correct! You need to return the filter on x, or shorten it.

const filteredAutor = parsedAutor.map((x) => x.filter((y) => y !== ''));

Or

const filteredAutor = parsedAutor.map((x) => {
    return x.filter((y) => y !== '');
});
Mathew Berg
  • 28,084
  • 11
  • 68
  • 89
1

You Need return the filter on x, or shorten it. return a value from your map() callback, either by using the return statement or removing the braces to make it an expression arrow function.

const filteredAutor = parsedAutor.map((x) => x.filter((y) => y !== ''));

or

const filteredAutor = parsedAutor.map((x) => {
    return x.filter((y) => y !== '');
});
Samir Lakhani
  • 665
  • 10
  • 18
1

You need to return a value from your map() callback, either by using the return statement or removing the braces to make it an expression arrow function.

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
1

You need to return the value from map.

const filteredAutor = parsedAutor.map((x) => {
   return x.filter((y) => y !== '');
});

Or else just do

const filteredAutor = parsedAutor.map(x => x.filter(y => y !== ''));
Archie
  • 901
  • 4
  • 11
0

may be this one ?

let myDoubleArray = [ [ 'Gladson de Lima Cameli', '', 'CPF 434.611.072-04' ]
                    , [ '30/05/2018 - Vara de Delitos de Roubo e Extorsão' ]
                    , [ 'Gladson de Lima Cameli', '', 'CPF 434.611.072-04' ]
                    , [ '29/02/2016 - 1ª Vara Criminal' ]
                    , [ 'Gladson de Lima Cameli', '', 'CPF 434.611.072-04' ]
                    , [ '18/02/2016 - 3º Juizado Especial Cível' ]
                    , [ 'Gladson de Lima Cameli', '', 'CPF 434.611.072-04' ]
                    , [ '18/02/2016 - 3º Juizado Especial Cível' ]
                    ]
// remove empty elements

for (let inArr of myDoubleArray)
  {
  for(let i = inArr.length;i--;) {  if (inArr[i]==='') inArr.splice(i,1)  }
  }

console.log(myDoubleArray)
Mister Jojo
  • 16,804
  • 3
  • 16
  • 39