I'm sorry if this is just common knowledge but I was thinking of a more advanced way of getting an object from an array than
for(var i = 0; i < array.length; i++)
{
if(i.id == idfromfunction)
{
dostuff();
}
}
so what I was thinking was instead do something like this
keyValuePairs = {};
function add(data,Id)
{
keyValuePairs[Id] = data
}
function remove(Id)
{
delete keyValuePairs[Id];
}
then data could be retrieved by simply
keyValuePairs[Id]
and iterate though the data with ( or using newer versions of this from this answer How to get all properties values of a Javascript Object (without knowing the keys)?)
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var val = obj[key];
// use val
}
}
or
function doLoop(callback)
{
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var val = obj[key];
callback(val, key);
}
}
}
then use by
doLoop(function(val, key){
dostuff(val);
or
if(key == idfromfunction)
{
dostuff(val);
}
});
now I'm not sure if this is actually what I was supposed to be doing all along or if this is common knowledge but honestly I only recently even found out about the delete keyword
So I guess the question is, is this a valid way to do key/value pairing (as a almost type of array)?
And why don't people just use this instead of arrays when making 3rd party plugins or why haven't I seen it before? ( is it much slower? ext )
I'm a bit busy now but provided no one mentions to me that this has been heavily documented already (or I'm just wrong in my thinking), i will do some experiments and post back.