0

I have an array in which the items are stored using other elements' ID values (not integers). Upon clicking a button I want to be able to change the boolean values of each item in this array to 'false' in one swoop. I'm assuming this can't be done with a loop since I'm only familiar with a loop using integer incrementation. Is there a way to change all of these values at once or do I just need to rethink this?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Logik
  • 13
  • 7
  • Yes, it's possible. Use `.forEach` for that. – Alma Do Aug 15 '13 at 14:24
  • Or the more compatible for...in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty – mplungjan Aug 15 '13 at 14:28
  • would help if you can post the sample of how this array looks – Shaunak Aug 15 '13 at 14:28
  • possible duplicate of [How do I enumerate the properties of a javascript object?](http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object) – mplungjan Aug 15 '13 at 14:29
  • an array with non-int indexes? sounds like an array that should probably be an object. – Kevin B Aug 15 '13 at 14:40
  • @KevinB yes and no. In JavaScript the separation becomes grey. Check my answer below. – Shaunak Aug 15 '13 at 14:41
  • It's more likely that OP confused object as array. I.e. OP hasn't really learnt basic JavaScript and somehow thinks that e.g. `foo["stringkey"]="bar"` represents an array item. So, throughout the whole question the misplaced term "array" must be replaced by "object" in order to understand (and answer) it better. – BalusC Aug 15 '13 at 14:41

1 Answers1

0

Because you didnt exactly post how your array looks like I assume a structure. This code will reset all boolean to false:

var array = {
    "id1":"foo",
    "id2":"bar",
    "id3":true,
    "id4":1,
    "id5":false,
};

$.each(array,function(index,obj){
    if(typeof obj === 'boolean' ) array[index] = false;
});

console.log(array);

Here is a jsfiddle (Check console log)

Trick to understand this is that an array in javascript is also an object with numeric keys. The keys don't have to be numeric though, and you can still access the array using non-numeric key. for example using the array above, array["id1"] is perfectly valid in javascript. It is equivalent of array.id1

UPDATE

The 'array' variable is not really an array here but an object. Please read @BalusC's comment below for details.

Shaunak
  • 17,377
  • 5
  • 53
  • 84
  • Also here, this is technically not an array. The `var array = {}` is misleading. I suggest to go through a sane JS tutorial once again to grasp the terminology. – BalusC Aug 15 '13 at 14:44
  • Right, but that's only because arrays ARE objects. you can do the same thing with the date object because it also is an object. http://jsfiddle.net/ANCMS/ Just be cause it works doesn't mean it's right. – Kevin B Aug 15 '13 at 14:45
  • Completely agree. That's what i tried to explain in the end. For this particular questions though, this seems to be the solution doesnt it? – Shaunak Aug 15 '13 at 14:47
  • @BalusC Agreed again. I used it purposely so person that asked the question can make that distinction. and understand that arrays in JS can be created like that. – Shaunak Aug 15 '13 at 14:48