innis
innis

Reputation: 380

How to remove empty elements from nested arrays

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.

Upvotes: 0

Views: 986

Answers (5)

Mister Jojo
Mister Jojo

Reputation: 22320

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)

Upvotes: 0

Archie
Archie

Reputation: 911

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 !== ''));

Upvotes: 1

SLaks
SLaks

Reputation: 887449

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.

Upvotes: 1

Samir Lakhani
Samir Lakhani

Reputation: 743

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 !== '');
});

Upvotes: 1

Mathew Berg
Mathew Berg

Reputation: 28750

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 !== '');
});

Upvotes: 1

Related Questions