Here is the example of what I am doing:
var size = new Array("S", "M", "L", "XL", "XXL"); var color = new Array("Red", "Blue", "Green", "White", "Black"); var options = new Array( size, color);
I am doing a loop select form thingies which work good, but I want to fetch the Array child name, in this case - size or color. When I am doing alert(options[0]), I get the whole elements of array. But for some specific case, I want to get only the array name, which is size/color like I have said already. Is there way to achieve that?
To access the elements of the inner arrays, you simply use two sets of square brackets. For example, pets[1][2] accesses the 3rd element of the array inside the 2nd element of the pets array. If you're used to a language like C, you may be wondering if you can create multi-dimensional arrays in JavaScript.
Nested Array in JavaScript is defined as Array (Outer array) within another array (inner array). An Array can have one or more inner Arrays. These nested array (inner arrays) are under the scope of outer array means we can access these inner array elements based on outer array object name.
An array is an ordered collection of values: each value is called an element, and each element has a numeric position in the array, known as its index. JavaScript lets us create arrays inside array called Nested Arrays.
Each variable or object in an array is called an element. Unlike stricter languages, such as Java, you can store a mixture of data types in a single array. For example, you could have array with the following four elements: an integer, a window object, a string and a button object.
I would create an object like this:
var options = { size: ["S", "M", "L", "XL", "XXL"], color: ["Red", "Blue", "Green", "White", "Black"] }; alert(Object.keys(options));
To access the keys individualy:
for (var key in options) { alert(key); }
P.S.: when you create a new array object do not use new Array
use []
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With