将数组推送到另一个数组js

时间:2012-08-20 09:56:05

标签: javascript arrays

我想在js / jquery中执行此操作:

var events = new Array();
var $event1 = [id, name, starttime, startposition];
var $event2 = [id2, name2, starttime2, startposition2];
var $event3 = [id3, name3, starttime3, startposition3];
events["fistcolumn"].push($event1, $event2, $event3);

这不起作用......你能告诉我怎么做吗?

5 个答案:

答案 0 :(得分:1)

var events = {}, //lets try that with an object?
    event1 = [id, name, starttime, startposition],
    event2 = [id2, name2, starttime2, startposition2],
    event3 = [id3, name3, starttime3, startposition3];

//lets join the arrays and stick them in the object
events["fistcolumn"] = event1.concat(event2).concat(event3);  

//now events.fistcolumn (kinky name) contains all the values from the arrays

if (events.fistcolumn[5] == name2) //true

答案 1 :(得分:0)

javascript中没有关联数组,您可以使用对象:

var events = {};
events["firstcolumn"] = event1;

另外,在你的vars面前丢失$,即php语法:)

更新

对于您的活动,您可能还应该使用对象:

var event2 = {     id:id2,     名称:name2,     starttime:starttime2,     ... };

答案 2 :(得分:0)

这可能有用。因为如果你想这样做,

 events["fistcolumn"] = events1;

事件必须是对象。

events.push($event1);
events.push($event2);
events.push($event3);

答案 3 :(得分:0)

var events = new Array(); 
var $event1 = ['id', 'name', 'starttime', 'startposition']; 
var $event2 = ['id2', 'name2', 'starttime2', 'startposition2']; 
var $event3 = ['id3', 'name3', 'starttime3', 'startposition3']; 
events["fistcolumn"] = $event1.concat($event2).concat($event3);

我将这些变量更改为字符串仅用于测试目的,您可以将其更改回来。

答案 4 :(得分:0)

var events = {};  // events object

var $event1 = [id, name, starttime, startposition];
var $event2 = [id2, name2, starttime2, startposition2];
var $event3 = [id3, name3, starttime3, startposition3];

然后你可以这样做

events["fistcolumn"] = [$event1, $event2, $event3];

或者像这样

events["fistcolumn"] = [];
events["fistcolumn"].push($event1);
events["fistcolumn"].push($event2);
events["fistcolumn"].push($event3);