I want to do this in 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);
this doesn't work... can you please tell me how to do it?
Dirty-flow
2,30011 gold badges30 silver badges49 bronze badges
asked Aug 20, 2012 at 9:56
Wassim Gr
5982 gold badges8 silver badges17 bronze badges
-
You've posted no jQuery code, so not sure why you tagged this as jquery and not javascript.Anthony Grist– Anthony Grist2012年08月20日 10:04:45 +00:00Commented Aug 20, 2012 at 10:04
5 Answers 5
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
answered Aug 20, 2012 at 10:02
adeneo
319k29 gold badges410 silver badges392 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
There are no associative arrays in javascript, you can use an object:
var events = {};
events["firstcolumn"] = event1;
Also, lose the $ in front of your vars, that is php syntax :)
Update:
For your events, you should probably also use objects:
var event2 = { id: id2, name: name2, starttime: starttime2, ... };
answered Aug 20, 2012 at 9:58
Asciiom
9,9757 gold badges42 silver badges59 bronze badges
1 Comment
Beetroot-Beetroot
"no associative arrays in javascript"? Sort of, but
var foo = []; foo['bar'] = "everything is fine"; alert(foo['bar']); does not give an error. bar actually becomes a property of the array but outwardly it amounts to the appearance of association, hence indexed and associative behaviour can be exhibited in one array simultaneously.This may work. Because if you want to do like below,
events["fistcolumn"] = events1;
events must be an Object.
events.push($event1);
events.push($event2);
events.push($event3);
answered Aug 20, 2012 at 10:00
Ramaraj Karuppusamy
2,3605 gold badges24 silver badges36 bronze badges
Comments
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);
I changed those variables to string for testing purposes only, you could change it back.
answered Aug 20, 2012 at 10:04
agou
7381 gold badge10 silver badges24 bronze badges
Comments
var events = {}; // events object
var $event1 = [id, name, starttime, startposition];
var $event2 = [id2, name2, starttime2, startposition2];
var $event3 = [id3, name3, starttime3, startposition3];
then you can either do like this
events["fistcolumn"] = [$event1, $event2, $event3];
or like this
events["fistcolumn"] = [];
events["fistcolumn"].push($event1);
events["fistcolumn"].push($event2);
events["fistcolumn"].push($event3);
answered Aug 20, 2012 at 10:40
Diode
25.2k8 gold badges43 silver badges52 bronze badges
Comments
lang-js