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?
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 );
你可以使用像这样的合并功能
var newArray = $.merge($.merge([], cString), test);
除了patrick所描述的push
之外,如果你想创建一个新列表而不是更改旧列表,你可以将数组与Array#concat
一起添加:
var newArray= cString.concat([['4','Stackoverflow','stackoverflow.com']]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.