javascript - In a JS array of objects, should I use a keys to identify each object? -
I'm still learning JavaScript patterns, and I'm curious about it.
If I have an array of objects, should not I use the keys of objects (eg A), or their keys (example B)?
An array of objects without any key:
<(id: 24325, name: "Jimi", gender: "male"), "male", weapon_off_choice: "kangaroo cannon" }, {ID: 24325, Name: "Jimmy", Gender: "Male", \ "Sushi Blaster \"}, ...]or as unique ID Key An array of objects to use:
var soylent_green_candidates = [12341: {name: "don", gender: "male", weapon_of_choice: "kangaroo cannon"}, 24325: {name: "Jimmy", Gender: "Male", Weapon_of_choice: "Sushi Blaster"}, ...]
Your second example is invalid, so that the question should sleep.
In Javascript you have the option of arrays , which is sorted but essentially "key is low". Each array entry can be reached only for its numerical index, period.
The other options are objects , which do not have any guaranteed order, but they are imposed on the basis of criteria.
:
(Note: When you can arrays arbitrarily numerical index for keys based on order and requirement If you use, you can not do the literal syntax [..]
, and this is usually not a good idea. If you need a clear key, then you want the objects.)
Comments
Post a Comment