简体   繁体   English

从数组中删除未定义的元素

[英]Remove undefined elements from array

I have a table where I pull data and add it to an array of arrays. 我有一个表,我拉数据并将其添加到数组数组。 The problems is if one of the table cells is empty it appears in the array as "undefined". 问题是如果其中一个表格单元格为空,则它在数组中显示为“未定义”。 I tried using an if the last element is undefined if so using .pop() the element should be removed. 如果使用.pop(),我尝试使用if,如果最后一个元素是未定义的,则应删除该元素。 I still get undefined elements. 我仍然得到未定义的元素。 Here's my code and live demo 这是我的代码和现场演示

HTML : HTML:

<table id="contactlisttable">
    <tr>
        <th>Name</th>
        <th>Title</th>
        <th>Phone</th>
    </tr>
    <tr>
        <td class="contactlist contactlistlastfirst">Joey</td>
        <td class="contactlist contactlisttitle">webdesigner</td>
        <td class="contactlist contactlistphone"></td>
    </tr>
    <tr>
        <td class="contactlist contactlistlastfirst">Anthony</td>
        <td class="contactlist contactlisttitle">webdesigner</td>
        <td class="contactlist contactlistphone">5555555</td>
    </tr>
</table> 


JavaScript : JavaScript:

 //IE9+ compatable solution $(function(){ var results = [], row; $('#contactlisttable').find('th, td').each(function(){ if(!this.previousElementSibling){ //New Row? row = []; results.push(row); if($(this) === 'undefined'){//Remove undefined elements row.pop(); } } row.push(this.textContent || this.innerText); //Add the values (textContent is standard while innerText is not) }); console.log(results); }); 

jsFiddle Demo

Instead of doing the conditional statements, just take advantage of your html structure. 而不是做条件语句,只需利用您的HTML结构。 First select by the table rows, and then iterate the child td or th elements. 首先按表行选择,然后迭代子tdth元素。 You can also take advantage of jQuery's text instead of doing the feature detection. 您还可以利用jQuery的text而不是进行特征检测。 jQuery's text will be more reliable. jQuery的文本会更可靠。

var results = [];
$('#contactlisttable tr').each(function(){
 var row = [];
 $(this).find('td,th').each(function(){
     row.push($(this).text());
 });
 results.push(row);
});
console.log(results);

Instead of pushing and popping if it doesn't match, don't push in the first place. 如果它不匹配,而不是推动和弹出,不要先推入。

Updated from your jsfiddle: 从您的jsfiddle更新:

//IE9+ compatable solution
$(function(){
    var results = [], row; 
    $('#contactlisttable').find('th, td').each(function(){
        if(!this.previousElementSibling && typeof(this) != 'undefined'){ //New Row?
            row = []; 
            results.push(row); 
        }
        row.push(this.textContent || this.innerText); //Add the values (textContent is standard while innerText is not)       
    }); 
    console.log(results); 
}); 

You can also avoid adding undefined (or actually empty) elements this way: 您还可以避免以这种方式添加未定义(或实际为空)的元素:

$('#contactlisttable').find('th, td').each(function(){
    if(!this.previousElementSibling){ //New Row?
        row = [];
        results.push(row);
    }
    if(!(this.textContent == '')){
        row.push(this.textContent || this.innerText);            
    }
}); 

To know if something is undefined dont just compare to "undefined" , use typeof(). 要知道某些内容是否未定义,只要与"undefined"进行比较,请使用typeof()。

So you want to do : 所以你想做:

if (typeof(this) === "undefined")

You could add a compact method like in underscore and lodash... 你可以添加一个紧凑的方法,如下划线和lodash ...

Array.prototype.compact = function() {
    return this.filter(function(x){
        return x !== undefined;
    });
}

console.log([1,2, '', undefined, 'e', undefined].compact()); // [ 1, 2, '', 'e' ]

You should probably add a check for a native implementation of compact as well, since you are overriding a native prototype with this. 您可能应该为compact的本机实现添加一个检查,因为您用此覆盖了本机原型。

Or, just 要不就

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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