Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Push into an Array inside an Object?

Tags:

Questions:

  1. How can I push an Array into another Array located inside an Object?

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.

like image 446
Coder 314 Avatar asked Jun 19 '14 22:06

Coder 314


People also ask

How do you push to an array inside an object?

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.

Can you put an array inside an object JavaScript?

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.

Can I use push in object JavaScript?

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.

Can we push array inside array in JavaScript?

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.


1 Answers

obj.arrayOne.push(arrayLetters); 

or

obj['arrayOne'].push(arrayLetters); 
like image 130
poe123 Avatar answered Oct 06 '22 00:10

poe123