user3259138
user3259138

Reputation: 441

Insert Array into an Array of Arrays Javascript

I've tried splice, concat, apply, call, etc.

All I am trying to do is take an array like this:

[["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],
 ["test","1","","","","1","0","1","0","0","0","",""],
 ["test3","","","","","","0","0","0","0","0","",""],
 ["","","","","","","0","0","0","0","0","",""],
 ["","","","","","","0","0","0","0","0","",""]]

And add something like this inside of the above array:

["2018-03-06 04:14:59", "T", "", "", "0"]

The end result would look like this:

[["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],
 ["test","1","","","","1","0","1","0","0","0","",""],
 ["test3","","","","","","0","0","0","0","0","",""],
 ["","","","","","","0","0","0","0","0","",""],
 ["","","","","","","0","0","0","0","0","",""],
 ["2018-03-06 04:14:59", "T", "", "", "0"]]

Thanks in advance. This has stumped me.

Upvotes: 0

Views: 85

Answers (6)

Sanjeev S
Sanjeev S

Reputation: 1163

Store the elements you want to push in a variable

the use push()

arr.push(ele);

this will automatically increase the index and inserts it in the last

Upvotes: 0

Suhosauge
Suhosauge

Reputation: 44

You can try using Array.push() like this

var datas = [["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],["test","1","","","","1","0","1","0","0","0","",""],["test3","","","","","","0","0","0","0","0","",""],["","","","","","","0","0","0","0","0","",""],["","","","","","","0","0","0","0","0","",""]];

then

datas.push(["2018-03-06 04:14:59", "T", "", "", "0"])

Upvotes: 1

guijob
guijob

Reputation: 4488

You can use push() as already mentioned by others in order to add a element to an existing array:

arr1.push(ele); // arr1 now has ele at last index

Either you can make use of concat(), which doesn't mutates original array and return a brand new one:

var arr2 = arr1.concat(ele) // arr1 remains the same and arr2 has arr1 with ele at las index

It depends on your needs.

Upvotes: 2

Hassan Imam
Hassan Imam

Reputation: 22534

You can use array#push() to add a new element to an array.

var data = [["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],["test","1","","","","1","0","1","0","0","0","",""],["test3","","","","","","0","0","0","0","0","",""],["","","","","","","0","0","0","0","0","",""],["","","","","","","0","0","0","0","0","",""]],
    arr = ["2018-03-06 04:14:59", "T", "", "", "0"];
data.push(arr);
console.log(data);

Upvotes: 1

Sudheesh Singanamalla
Sudheesh Singanamalla

Reputation: 2297

You need to use the push() method to achieve your goal. Here's a snippet:

var initial = [
    ["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],
    ["test","1","","","","1","0","1","0","0","0","",""],
    ["test3","","","","","","0","0","0","0","0","",""],
    ["","","","","","","0","0","0","0","0","",""],
    ["","","","","","","0","0","0","0","0","",""]
];

var new_array = ["2018-03-06 04:14:59", "T", "", "", "0"];
initial.push(new_array);

initial now contains the new array of arrays you're looking for.

Upvotes: 1

brk
brk

Reputation: 50291

Use push method which adds elements to the end of an array.

var originalArray = [
  ["alksjdflskdj", "2", "1.33", "1.30", "", "5", "1", "1", "1", "1", "1", "0", "", "Other notes"],
  ["test", "1", "", "", "", "1", "0", "1", "0", "0", "0", "", ""],
  ["test3", "", "", "", "", "", "0", "0", "0", "0", "0", "", ""],
  ["", "", "", "", "", "", "0", "0", "0", "0", "0", "", ""],
  ["", "", "", "", "", "", "0", "0", "0", "0", "0", "", ""]
]

var ArrayToPush = ["2018-03-06 04:14:59", "T", "", "", "0"];

originalArray.push(ArrayToPush);
console.log(originalArray)

Upvotes: 1

Related Questions