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]
];
How can i loop through this array to get all 'false' values from that array?
3 Answers 3
use a loop
for (var i=0;i<linkArray.length;i++)
{
document.write(linkArray[i][1] + "<br>");
}
answered Mar 14, 2013 at 11:35
AboQutiesh
1,7162 gold badges9 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
If you just want a list of boolean values, the you can use this:
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
}
}
answered Mar 14, 2013 at 11:35
musefan
48.6k21 gold badges145 silver badges193 bronze badges
3 Comments
Fabien
Do not use
for ... in with regular arrays.Fabien
It sometimes breaks and is thus considered bad style. You can read stackoverflow.com/questions/500504/… for example.
Mark Walters
@musefan have a look here stackoverflow.com/questions/500504/…
Judging by the variable name, the string values and the structure of your array, it might be a better approach to use object instead of array:
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
};
Now you can get the array of the boolean values like this:
var ret = [];
for (var propertyName in linkObject) {
ret.push(linkObject[propertyName]);
}
But you can also get to a specific value like this:
linkObject['boothsizeDiv_link']
which yields false.
answered Mar 14, 2013 at 11:48
linski
5,0943 gold badges24 silver badges35 bronze badges
Comments
lang-js