检查特定的数组元素序列是否为空

时间:2015-08-10 17:30:36

标签: javascript arrays loops

MBIG

我有上面显示的这个数组。前八个元素是标题。所以我将删除那些使用拼接。

我需要检查元素["Order", "Id", "Comment", "Id_type", "UbAction", "Ub_Factor", "LbAction","Lb_Factor", "6", "12321", "3rd test", "Industry", "0.23", "true", "0", "false", "22", "sss", "4", "Sector", "0", "true", "0.565676", "true", "99", "277906", "", "Sector", "1", "true", "0", "true", "3", "416921", "correction check", "Industry", "1", "true", "0", "false"] 8,16,249,17,25是否为空。我如何动态循环遍历这些元素。元素序列很奇怪。有人可以建议如何检查这些元素编号是否是当前正在循环的元素编号。

3 个答案:

答案 0 :(得分:1)

谢谢各位回复。我做了一点工作,下面的代码非常适合我......

var count = 0;
var array2= [0,1,3,4,6];

function checkArray5(cell_data){
            for(var i=8;i<cell_data.length; i++) {
                            for (var j=0;j<5;j++)
                                        { 
                                        if (i%8 == array2[j])
                                                        {
                                                        if(cell_data[i] ==="") {console.log(count++); 
                                                        }

                                        }
                            }
            }


}
checkArray5(array);

答案 1 :(得分:0)

这是我的建议:

var myArr = ["Order", "Id", "Comment", "Id_type", "UbAction", "Ub_Factor", "LbAction", "Lb_Factor",
    "6", "12321", "3rd test", "Industry", "0.23", "true", "0", "false",
    "22", "sss", "4", "Sector", "0", "true", "0.565676", "true",
    "99", "277906", "", "Sector", "1", "true", "0", "true",
    "3", "416921", "correction check", "Industry", "1", "true", "0", "false"];
// added or remove elements to be checked if empty.
var emptyElementChk = [8, 9, 11, 16, 17, 19, 24, 25, 27];

for (var i = 0; i < emptyElementChk.length; i++) {
    //All checked array listed
    console.log("Key : " + emptyElementChk[i] + " value: " + myArr[emptyElementChk[i]]);
    if (myArr[emptyElementChk[i]] == "") {
        // empty arrays action
    } else {
        // none empty arrays action
    }
};

答案 2 :(得分:0)

使用%运算符分别搜索返回0,1和3的索引:

var table = ["Order", "Id", "Comment", "Id_type", "UbAction", "Ub_Factor", "LbAction","Lb_Factor", 
"6", "12321", "3rd test", "Industry", "0.23", "true", "0", "false", 
"22", "sss", "4", "Sector", "0", "true", "0.565676", "true",
"99", "277906", "", "Sector", "1", "true", "0", "true", 
"3", "416921", "correction check", "Industry", "1", "true", "0", "false"];
var index = 0;
var columns = 8;
var cells = table.slice(columns);
var len = cells.length;

for (;index < len; index++) {
  var mod = index % columns;
  switch (mod) {
    case 0:
    case 1:
    case 3:
     isEmpty(cells[index]);
    break;
  }
}