简体   繁体   中英

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.

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

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

您需要从map()回调中返回一个值,方法是使用return语句或删除大括号以使其成为表达式箭头函数。

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM