简体   繁体   中英

How to remove empty array values ("") from an array?

I have an two dimensional array, generated from a html table with jQuery, but some values are empty so "" is displayed.

How can I remove the empty values?

  <table>    
    <tr>
      <th>1A</th>
      <th>1B</th>
      <th>1C</th>
    </tr>
    <tr>
      <td>2A</td>
      <td>2B</td>
      <td>2C</td>
    </tr>
    <tr>
      <td></td>
      <td>3B</td>
      <td>3C</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>4C</td>
    </tr>
  </table>
<script>
    var columns = $('tr').first().children().map(function(i) {
        return [
            $('tr').map(function(){
                return $(this).children().eq(i).text()
            }).get()
        ]
    }).get();
<script>

I already tried following code:

for( var i = 0; i < columns[0].length; i++){ 
   if ( columns[0][i] === "") {
    columns[0].splice(i, 1); 
   }
}

It worked for some empty values, but not all of them got removed for some reason.

Output: https://imgur.com/e7BAdQK

I have an two dimensional array, generated from a html table with jQuery, but some values are empty so "" is displayed.

How can I remove the empty values?

  <table>    
    <tr>
      <th>1A</th>
      <th>1B</th>
      <th>1C</th>
    </tr>
    <tr>
      <td>2A</td>
      <td>2B</td>
      <td>2C</td>
    </tr>
    <tr>
      <td></td>
      <td>3B</td>
      <td>3C</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>4C</td>
    </tr>
  </table>
<script>
    var columns = $('tr').first().children().map(function(i) {
        return [
            $('tr').map(function(){
                return $(this).children().eq(i).text()
            }).get()
        ]
    }).get();
<script>

I already tried following code:

for( var i = 0; i < columns[0].length; i++){ 
   if ( columns[0][i] === "") {
    columns[0].splice(i, 1); 
   }
}

It worked for some empty values, but not all of them got removed for some reason.

Output: https://imgur.com/e7BAdQK

I have an two dimensional array, generated from a html table with jQuery, but some values are empty so "" is displayed.

How can I remove the empty values?

  <table>    
    <tr>
      <th>1A</th>
      <th>1B</th>
      <th>1C</th>
    </tr>
    <tr>
      <td>2A</td>
      <td>2B</td>
      <td>2C</td>
    </tr>
    <tr>
      <td></td>
      <td>3B</td>
      <td>3C</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>4C</td>
    </tr>
  </table>
<script>
    var columns = $('tr').first().children().map(function(i) {
        return [
            $('tr').map(function(){
                return $(this).children().eq(i).text()
            }).get()
        ]
    }).get();
<script>

I already tried following code:

for( var i = 0; i < columns[0].length; i++){ 
   if ( columns[0][i] === "") {
    columns[0].splice(i, 1); 
   }
}

It worked for some empty values, but not all of them got removed for some reason.

Output: https://imgur.com/e7BAdQK

I have an two dimensional array, generated from a html table with jQuery, but some values are empty so "" is displayed.

How can I remove the empty values?

  <table>    
    <tr>
      <th>1A</th>
      <th>1B</th>
      <th>1C</th>
    </tr>
    <tr>
      <td>2A</td>
      <td>2B</td>
      <td>2C</td>
    </tr>
    <tr>
      <td></td>
      <td>3B</td>
      <td>3C</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>4C</td>
    </tr>
  </table>
<script>
    var columns = $('tr').first().children().map(function(i) {
        return [
            $('tr').map(function(){
                return $(this).children().eq(i).text()
            }).get()
        ]
    }).get();
<script>

I already tried following code:

for( var i = 0; i < columns[0].length; i++){ 
   if ( columns[0][i] === "") {
    columns[0].splice(i, 1); 
   }
}

It worked for some empty values, but not all of them got removed for some reason.

Output: https://imgur.com/e7BAdQK

I have an two dimensional array, generated from a html table with jQuery, but some values are empty so "" is displayed.

How can I remove the empty values?

  <table>    
    <tr>
      <th>1A</th>
      <th>1B</th>
      <th>1C</th>
    </tr>
    <tr>
      <td>2A</td>
      <td>2B</td>
      <td>2C</td>
    </tr>
    <tr>
      <td></td>
      <td>3B</td>
      <td>3C</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>4C</td>
    </tr>
  </table>
<script>
    var columns = $('tr').first().children().map(function(i) {
        return [
            $('tr').map(function(){
                return $(this).children().eq(i).text()
            }).get()
        ]
    }).get();
<script>

I already tried following code:

for( var i = 0; i < columns[0].length; i++){ 
   if ( columns[0][i] === "") {
    columns[0].splice(i, 1); 
   }
}

It worked for some empty values, but not all of them got removed for some reason.

Output: https://imgur.com/e7BAdQK

I have an two dimensional array, generated from a html table with jQuery, but some values are empty so "" is displayed.

How can I remove the empty values?

  <table>    
    <tr>
      <th>1A</th>
      <th>1B</th>
      <th>1C</th>
    </tr>
    <tr>
      <td>2A</td>
      <td>2B</td>
      <td>2C</td>
    </tr>
    <tr>
      <td></td>
      <td>3B</td>
      <td>3C</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>4C</td>
    </tr>
  </table>
<script>
    var columns = $('tr').first().children().map(function(i) {
        return [
            $('tr').map(function(){
                return $(this).children().eq(i).text()
            }).get()
        ]
    }).get();
<script>

I already tried following code:

for( var i = 0; i < columns[0].length; i++){ 
   if ( columns[0][i] === "") {
    columns[0].splice(i, 1); 
   }
}

It worked for some empty values, but not all of them got removed for some reason.

Output: https://imgur.com/e7BAdQK

I have an two dimensional array, generated from a html table with jQuery, but some values are empty so "" is displayed.

How can I remove the empty values?

  <table>    
    <tr>
      <th>1A</th>
      <th>1B</th>
      <th>1C</th>
    </tr>
    <tr>
      <td>2A</td>
      <td>2B</td>
      <td>2C</td>
    </tr>
    <tr>
      <td></td>
      <td>3B</td>
      <td>3C</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>4C</td>
    </tr>
  </table>
<script>
    var columns = $('tr').first().children().map(function(i) {
        return [
            $('tr').map(function(){
                return $(this).children().eq(i).text()
            }).get()
        ]
    }).get();
<script>

I already tried following code:

for( var i = 0; i < columns[0].length; i++){ 
   if ( columns[0][i] === "") {
    columns[0].splice(i, 1); 
   }
}

It worked for some empty values, but not all of them got removed for some reason.

Output: https://imgur.com/e7BAdQK

i'm pretty surprised nobody gave the right accurate answer here.

in all of the Boolean based expressions here, you'd filter the results that you'd want to keep. for instance - "0" , etc.

array.filter(item => item !== '')

I have an two dimensional array, generated from a html table with jQuery, but some values are empty so "" is displayed.

How can I remove the empty values?

  <table>    
    <tr>
      <th>1A</th>
      <th>1B</th>
      <th>1C</th>
    </tr>
    <tr>
      <td>2A</td>
      <td>2B</td>
      <td>2C</td>
    </tr>
    <tr>
      <td></td>
      <td>3B</td>
      <td>3C</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td>4C</td>
    </tr>
  </table>
<script>
    var columns = $('tr').first().children().map(function(i) {
        return [
            $('tr').map(function(){
                return $(this).children().eq(i).text()
            }).get()
        ]
    }).get();
<script>

I already tried following code:

for( var i = 0; i < columns[0].length; i++){ 
   if ( columns[0][i] === "") {
    columns[0].splice(i, 1); 
   }
}

It worked for some empty values, but not all of them got removed for some reason.

Output: https://imgur.com/e7BAdQK

In ES6, Lets say you have the following array:

arr = [1, 2, 3, '', false, '4'];

And you want to remove '' (Which is an empty value) from the array. You can do:

const filter = (...args) => args.filter(el => el !== '');
console.log(filter(...arr));
[1, 2, 3, false, "4"] // Output

Or Using Map (Regular JS)

const myArr = [1, 2, '', '4'];

noEmptyStringInThisArray = [];

myArr.map((elem) => {
    if (elem !== '') {
    noEmptyStringInThisArray.push(elem);
    }
})

console.log(noEmptyStringInThisArray);
// [1, 2, "4"]

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