0

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.

1
  • array_of_arrays.push(new_array)? Commented Mar 6, 2018 at 4:39

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I really appreciate your additional information
1

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)

3 Comments

This is exactly what I tried and it gave me "thearray.push is not a function"
@user3259138 i can help you if you can share the code which you have tried
So this code works, the issue was that I needed to use JSON.parse on the original array, because I was pulling it from local storage. I really appreciate your offer to help @brk
1

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.

Comments

1

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);

Comments

1

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"])

Comments

0

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

1 Comment

Thank you. I appreciate your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.