Loop over array and set all vars to false or true in javascript
I need to loop over my array and set all the vars to false or true. I have tried numerous options, none of which are working and it's clearly my lack of javascript syntax knowledge..Please take a look at this:
var closeAllCells = [IncomeOpen = "false",
RehabOpen = "fal开发者_如何学Gose",
AttendantCareOpen = "false",
HomeMaintenanceOpen = "false",
DependantCareOpen = "false",
IndexationOpen = "false",
DeathFuneralOpen = "false",
ComprehensiveOpen = "false",
CollisionOpen = "false",
LiabilityOpen = "false",
DCPDOpen = "false"];
So my thinking is that I can just loop over this as follows
for (var i=0;i<closeAllCells.length;i++)
{
closeAllCells[i] = true; // or false if I wished
}
In your example, you are creating an array that contains the values assigned to a bunch of global variables, the array looks like this:
[false, false, false, false, false, false, ...]
.
You are looking to use an object literal to store your values as properties an object, for example:
var data = {
IncomeOpen : false,
RehabOpen : false,
AttendantCareOpen : false,
HomeMaintenanceOpen : false,
DependantCareOpen : false,
IndexationOpen : false,
DeathFuneralOpen : false,
ComprehensiveOpen : false,
CollisionOpen : false,
LiabilityOpen : false,
DCPDOpen : false
};
for (var prop in data) {
if (data.hasOwnProperty(prop)) {
data[prop] = true;
}
}
As you can see, we are using a for...in
loop here, the purpose of this statement is to enumerate object properties.
You can access the individual properties also like this:
alert(data.IncomeOpen); // true, or
alert(data["RehabOpen"]); // true
You may wonder why we need to call the hasOwnProperty
method, well that's to ensure that only own properties (properties that exist physically on the object, the ones we defined) are enumerated, not any inherited property.
See also:
- Object literals
- The
for...in
statement
You are defining your array like an object kinda? it's an interesting notation, lol..
Your indices aren't in quotes or apostrophes and false should be by itself (not in quotes)
var closeAllCels = new Array();
closeAllCells['IncomeOpen'] = false;
closeAllCells['RehabOpen'] = false;
perhaps you were thinking of an object?
var closeAllCells = { IncomeOpen : false,
RehabOpen = false
};
as already stated by CMS, an object would be a great option for ya
your array is actually an associative array, or hash. and the correct syntax for declaring it is:
var closeAllCells = {
IncomeOpen: false,
RehabOpen: false
};
you can then iterate over each property in your object:
for (var property in closeAllCells) {
closeAllCells[property] = false;
}
once you have defined that the content of the array is going to hold a particular type of value you can loop over it to change the value, but until you define the type of content of an array you cant loop over it.
Rookie mistake, I think it's the string assignment that cocked me! Should be:
var closeAllCells = [IncomeOpen = false,
RehabOpen = false,
AttendantCareOpen = false,
HomeMaintenanceOpen = false,
DependantCareOpen = false,
IndexationOpen = false,
DeathFuneralOpen = false,
ComprehensiveOpen = false,
CollisionOpen = false,
LiabilityOpen = false,
DCPDOpen = false];
I think?
精彩评论