Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  85] [ 2]  / answers: 1 / hits: 60700  / 14 Years ago, sat, september 11, 2010, 12:00:00

I have an array, as below:



var cString =   [
['1','Techdirt','www.techdirt.com'],
['2','Slashdot','slashdot.org'],
['3','Wired','wired.com']
];


to this array I want to add another in the same format:



var test = ['4','Stackoverflow','stackoverflow.com']


I've tried using:



var newArray = $.merge(cString, test);


But console.log(newArray); outputs:



[►Array,►Array,►Array,'4','Stackoverflow','stackoverflow.com']


So I'm assuming that I'm missing something obvious. Or attempting something stupid...help?


More From » jquery

 Answers
10

jQuery is not needed for this. Just use the Array's .push() method to add it to the main array.



var test = ['4','Stackoverflow','stackoverflow.com']

cString.push( test );


What $.merge() does is it walks through the second array you pass it and copies its items one by one into the first.






EDIT:



If you didn't want to modify the original array, you could make a copy of it first, and .push() the new Array into the copy.



var cString =   [
['1','Techdirt','www.techdirt.com'],
['2','Slashdot','slashdot.org'],
['3','Wired','wired.com']
];

var test = ['4','Stackoverflow','stackoverflow.com']

var newArray = cString.slice();

newArray.push( test );

[#95653] Wednesday, September 8, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daytonm

Total Points: 519
Total Questions: 83
Total Answers: 89

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;