循环遍历2D数组以获取第二列值

时间:2013-03-14 11:31:22

标签: javascript arrays loops

var linkArray = [
['boothsizeDiv_link', false],
['furnishingsprovidedDiv_link', false],
['electricalDiv_link', false],
['rentalfurnishingsDiv_link', false],
['gesgraphicsDiv_link', false],
['geslaborDiv_link', false],
['contractorDiv_link', false],
['carpetingDiv_link', false],
['boothlightingDiv_link', false],
['javitsDiv_link', false],
['boothsealDiv_link', false],
['mannequinsDiv_link', false],
['calcDiv_link', false]
];

如何遍历此数组以从该数组中获取所有“false”值?

3 个答案:

答案 0 :(得分:3)

使用循环

for (var i=0;i<linkArray.length;i++)
{
     document.write(linkArray[i][1] + "<br>");

}

答案 1 :(得分:0)

如果你只想要一个布尔值列表,你可以使用它:

var falseValues = [];//this will be your list of false values

for(var i = 0; i < linkArray.length; i++){
    var link = linkArray[i];
    //test the value at array index 1 (i.e. the boolean part)
    if(!link[1]){
       falseValues.push(link[1]);//add the false to the list
    }
}

答案 2 :(得分:0)

根据变量名,字符串值和数组结构来判断,使用object而不是数组可能是更好的方法:

var linkObject = {
 boothsizeDiv_link: false,
 furnishingsprovidedDiv_link: false,
 electricalDiv_link: false,
 rentalfurnishingsDiv_link: false,
 gesgraphicsDiv_link: false,
 geslaborDiv_link: false,
 contractorDiv_link: false,
 carpetingDiv_link: false,
 boothlightingDiv_link: false,
 javitsDiv_link: false,
 boothsealDiv_link: false,
 mannequinsDiv_link: false,
 calcDiv_link: false
};

现在你可以得到这样的布尔值数组:

var ret = [];
for (var propertyName in linkObject) {
    ret.push(linkObject[propertyName]);
}

但你也可以得到这样的特定值:

linkObject['boothsizeDiv_link'] 

产生false