Questions:
Example Coding:
var myObj = { arrayOne: [], arrayTwo: [] }; var arrayLetters = ['A', 'B']; /************************************ obj[arrayOne].push(arrayLetters); RESULT { arrayOne: [['A', 'B']], arrayTwo: [] }; ************************************/
Comments:
Essentially, I would like to have an key index for my various arrays.
In order to push an array into the object in JavaScript, we need to utilize the push() function. With the help of Array push function this task is so much easy to achieve. push() function: The array push() function adds one or more values to the end of the array and returns the new length.
You can use Array inside object JavaScript. Not even Array anything inside JavaScript objects, including other objects, functions, etc can define. Array is an object, and you can have an object as a property of another object.
push can work on an object just fine, as this example shows. Note that we don't create an array to store a collection of objects. Instead, we store the collection on the object itself and use call on Array.
To add an array into an array in JavaScript, use the array. push() method. The push() function allows us to push an array into an array. We can add an array into an array, just like adding an element into the Array.
obj.arrayOne.push(arrayLetters);
or
obj['arrayOne'].push(arrayLetters);
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