problem understanding multidimensional arrays using javascript and json

Hi, In javascript I have a collection of several objects with a known numerical id and each one has several properties, this list is updated using ajax and json.
An example of the JSON structure is attached, I can then refer to anything by its id so data[42].time.  This means the array may just have a couple of entries i.e. 42 and 69.
This all works fine in firefox and safari but ie has some int. problems since I switched from jquery to prototype.
The only other way I have tried is creating a normal array i.e. data[0].id=42, then search through to find the right id, get the array position and refer to it like so. data[0].time.
Am I missing something? is there another way of doing things?

Thanks.
{"time":"23:19:28","vehicles":{ 32:{"id":  "32",
				"time": "18:54 7th Apr",
				"sp": "0",
				"ig": "0",
				"status": "0",
				"desc": "",
				"f1": ""
				 },49:{"id":  "49",
				"time": "23:17 8th Apr",
				"sp": "0",
				"ig": "0",
				"status": "Hired",
				"desc": "",
				"f1": ""
				 },}}

Open in new window

ewan69Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
to loop on all vehicles use this "array" : json["vehicles"]

and :
for(vehicule_id in json["vehicles"]) {
	alert( vehicule_id );
}

Open in new window

0
 
leakim971PluritechnicianCommented:
Hello ewan69,

You can get time with id directly with :


var time32 = json["vehicles"]["32"].time;
var time49 = json["vehicles"]["49"].time;

Open in new window

0
 
ewan69Author Commented:
But i want an array, sometimes i need to loop through all vehicles and perform a task.
0
 
BadotzCommented:
Or you could encapsulate the method directly into your JSON, then you could list the ids like this:

alert (jtime.list_ids());


var jtime = (function() {
	"time":"23:19:28",
	"vehicles":{ 
		32:{
			"id":  "32",
			"time": "18:54 7th Apr",
			"sp": "0",
			"ig": "0",
			"status": "0",
			"desc": "",
			"f1": ""
		},
		49:{
			"id":  "49",
			"time": "23:17 8th Apr",
			"sp": "0",
			"ig": "0",
			"status": "Hired",
			"desc": "",
			"f1": ""
		},
	}
	"list_all_ids": function() {
		for(var id in vehicles) {
			ra.push(vehicule_id);
		}
		return ra.split(''); // Comma-delimited list
	},
	// Exposed methods
	return {
		"list_ids":function() { return list_all_ids(); }
	};
})();

Open in new window

0
All Courses

From novice to tech pro — start learning today.