Assign Index And Values To A Multidimensional Array In Jquery
Solution 1:
here I want that index 9 is set as an array, but it adds 9 elements ,
I need that only one index is added i.e. 9 and its value is array
Actually, it doesn't add nine elements, just one. That one element is at index 8 (presumably, not 9 — remember indexes start at 0); indexes 0-7do not have values at all. This is because JavaScript standard arrays are inherently sparse (and aren't really arrays at all). More on this below.
length comes out to be 10
but it should be 1 as I have added only one index i.e. 9 & its an array
The length property isn't defined to be a count of the number of entries in the array. It's defined to be (at least) one greater than the highest array index. Because arrays can have missing entries, the length can be more (but not less) than the number of entries in the array.
Now, if you don't need the array-ness of the array object, you could just use a plain object instead. The only difference is that it doesn't have a length property, and doesn't have the various array-specific functions (forEach, etc.). To do that, just change your
varAllItem = newArray();
line to
var AllItem = {};
You can use the [] notation with objects as well as arrays (because arrays are just objects, the [] notation is an object thing, not an array thing), so your other code doesn't change
But again, the only difference is the length property and the various array functions.
More on how adding an entry at index 8 doesn't create entries at 0-7:
When you do this:
var a = [];
a[8] = "value";
All that happens is that entry 8 is set to the value and a.length is set to 9. Nothing else is added to the array.
console.log(0 in a); // false
console.log(1 in a); // false
...
console.log(7 in a); // false
console.log(8 in a); // true!People sometimes have trouble wrapping their heads around this, but the key to it is in the linked article above: Arrays aren't really arrays at all, they're just objects with a bit of magic thrown in. Array "indexes" are just property names, and array "entries" are just properties. So a[8] = "value" creates a property called "8" on the a object. It does not create any other properties.
When you read the value of a property that doesn't exist (on the object or its prototype), you get back the value undefined, but that doesn't mean the property exists, that's just how JavaScript is defined to deal with that situation (reading a property that doesn't exist).
The in operator (used above) and hasOwnProperty function let you test for whether a property exists at all (in looks at the ojbect and its prototype(s), hasOwnProperty only looks at the object).
Re your question below about your updated code, this line may or may not be a problem depending on what other code you have:
AllItem[CategoryCode][dropdownumber] = SelectedVal;
That's fine if you've already put an object or array at AllItem[CategoryCode]. If you haven't, it will cause an error.
If you know you've done that, then the line is fine.
If you know you haven't done that, then:
AllItem[CategoryCode] = [];
AllItem[CategoryCode][dropdownumber] = SelectedVal;
If you may or may not have done that, then:
if (!AllItem[CategoryCode]) {
AllItem[CategoryCode] = [];
}
AllItem[CategoryCode][dropdownumber] = SelectedVal;
(Or sometimes you'll see this:
(AllItem[CategoryCode] = AllItem[CategoryCode] || [])[dropdownumber] = SelectedVal;
but there's usually no need for that kind of thing, which is hard to read and hard to debug.)
Post a Comment for "Assign Index And Values To A Multidimensional Array In Jquery"