2

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.

Community
  • 1
  • 1
Popeye
  • 411
  • 3
  • 10
  • 1
    If you use an array you get helpful methods like `.filter()`, `.map()`, `.reduce()`, `.sort()`, etc. If you don't need to search for individual items in the list (but only ever process the whole list or add items to the end of it), then it makes sense to use an array. Otherwise, as a general rule (of course there are exceptions), use an object when you know keys will be unique, and use an array where keys are not unique or where list order matters. – nnnnnn Jun 08 '16 at 03:53

1 Answers1

3

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 then

for(var i = 0; i < array.length; i++)
{
    if(i.id == idfromfunction)
    {
      dostuff();
    }
}

You're right, there is a more "advanced" way. It is Array.prototype.find

var item = array.find(function(x) { return x.id === idFromFunction });

if (item === undefined)
  doSomething();
else
  doStuff(item);

Array.prototype.find will iterate through an array calling the provided procedure once per item. If the procedure returns true, that item is returned and iteration terminates. If the procedure never returns true, the resulting value will be undefined.

With your last bit of code, it looks like you were well on your way to discovering/implementing this function on your own. Good work.

Mulan
  • 129,518
  • 31
  • 228
  • 259