我如何添加一个数组来使用jQuery数组的数组? [英] How can I add an array to an array of arrays using jQuery?

查看:141
本文介绍了我如何添加一个数组来使用jQuery数组的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,如下:

  VAR的CString = [
            ['1','Techdirt的','www.techdirt.com'],
            ['2','Slashdot的','slashdot.org'],
            ['3','有线','wired.com']
            ];

这阵我想补充另一个相同的格式:

  VAR测试= ['4','#1','stackoverflow.com']

我已经尝试使用:

  VAR newArray = $ .merge(CString的,测试);

的console.log(newArray); 输出:

  [►Array,►Array,►Array,'4','#1','stackoverflow.com']

所以我的假设的,我失去了一些东西明显。或者尝试一些愚蠢的...帮助?


解决方案

不需要

jQuery的这一点。只需使用阵列的 .push()方法以将其添加到主阵列

  VAR测试= ['4','#1','stackoverflow.com']cString.push(试验);

什么 $。合并()确实是走过你通过它并复制其项目一个一个地进入第一,第二阵列。


编辑:

如果你不想修改原来的数组,你可以把它的一个副本第一和 .push()新阵列进入副本。

  VAR的CString = [
            ['1','Techdirt的','www.techdirt.com'],
            ['2','Slashdot的','slashdot.org'],
            ['3','有线','wired.com']
            ];变种测试= ['4','#2','stackoverflow.com']变种newArray = cString.slice();newArray.push(试验);

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

这篇关于我如何添加一个数组来使用jQuery数组的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆