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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
leakim971PluritechnicianCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.