D
d d
I have an array of objects that start out looking like this:
var ra=[
{name:"fred",someproperty:"hello fred"},
{name:"bob", someproperty:"hello bob"},
{name:"joe", someproperty:"hello joe"}
];
I want to be able to access the array by index number from some code,
and by the name property from other code, as if it had been defined like
this instead:
var ra=[];
ra["fred"] = {name:"fred",someproperty:"hello fred"};
ra["bob"] = {name:"bob", someproperty:"hello bob"};
ra["joe"] = {name:"joe", someproperty:"hello joe"};
];
I could choose either way, and use for(var x in ra) or use numerical
index and check the name property, but instead I did this:
var ra=[
{name:"fred",someproperty:"hello fred"},
{name:"bob", someproperty:"hello bob"},
{name:"joe", someproperty:"hello joe"}
];
for(var i=0;i<ra.length;i++)
ra[ra.name]=ra;
When I look at the array in visual studio I get this list:
[0]
[1]
[2]
["fred"]
["bob"]
["joe"]
and if I make a change to the contents of [0], they're reflected in the
["joe"] entry because ["joe"] is just a pointer to [0].
Perfect solution or a bad idea? I'll never have more than 10 in the
original array and it's not involved in any major heavy loops.
~dd
var ra=[
{name:"fred",someproperty:"hello fred"},
{name:"bob", someproperty:"hello bob"},
{name:"joe", someproperty:"hello joe"}
];
I want to be able to access the array by index number from some code,
and by the name property from other code, as if it had been defined like
this instead:
var ra=[];
ra["fred"] = {name:"fred",someproperty:"hello fred"};
ra["bob"] = {name:"bob", someproperty:"hello bob"};
ra["joe"] = {name:"joe", someproperty:"hello joe"};
];
I could choose either way, and use for(var x in ra) or use numerical
index and check the name property, but instead I did this:
var ra=[
{name:"fred",someproperty:"hello fred"},
{name:"bob", someproperty:"hello bob"},
{name:"joe", someproperty:"hello joe"}
];
for(var i=0;i<ra.length;i++)
ra[ra.name]=ra;
When I look at the array in visual studio I get this list:
[0]
[1]
[2]
["fred"]
["bob"]
["joe"]
and if I make a change to the contents of [0], they're reflected in the
["joe"] entry because ["joe"] is just a pointer to [0].
Perfect solution or a bad idea? I'll never have more than 10 in the
original array and it's not involved in any major heavy loops.
~dd