1

I have an array of data which stores an object with functions and other such info. I push these objects to the function for my draw function to execute.

But i do not know of a way to find a specific object in an array to remove it and thus stop drawing it.

For example i have an array structure like this:

var data  = {
            'fnc':function(){ 
                      updatePosition(spriteID); 
                      drawSprite(spriteID); 
             },
             'something':'here'
            };

var drawOrder= [];
drawOrder.push(data);

There are many functions in this array and they are pushed dynamically depending on what i wish to draw.

What would be the best way to find the index of one of these objects and remove it from the array in this case?

Sir
  • 7,735
  • 12
  • 72
  • 138
  • You want to find an object in your array by its `type` property? – Daniel Lizik Jan 09 '16 at 04:23
  • No, the entire object as a whole not just `type. That `type` is related to my draw function. Edited the code to avoid the confusion :) – Sir Jan 09 '16 at 04:24
  • why drawOrder.indexOf(objYouPush) will not work in your case? – Stefano Saitta Jan 09 '16 at 04:24
  • Sorry I don't understand your quesion. If you want `the best way to find the index`, just do `drawOrder[n].fnc()` – Daniel Lizik Jan 09 '16 at 04:26
  • @Daniel_L but `n` implies i already know the index in which case i can just splice it. – Sir Jan 09 '16 at 04:26
  • Yes, but your question does not state the criteria by which you wish to find the object in your array. If each object in your array has an `id` property, loop through the array and return the object whose id matches your criteria. – Daniel Lizik Jan 09 '16 at 04:27
  • Well i need to find that unique object i don't know how thats why i'm asking the question. – Sir Jan 09 '16 at 04:27
  • `drawOrder.indexOf(data)` is simple enough... should be `0` in the code above... – dandavis Jan 09 '16 at 04:28
  • I guess its similar though the accepted answer did not suggest it also applied to looking up entire objects ! But now i know :) – Sir Jan 09 '16 at 04:47
  • google for "javascript find element array" –  Jan 09 '16 at 05:00

2 Answers2

1

indexOf() returns the index in the array of the element you're searching for, or -1. So you can do:

var index = drawOrder.indexOf("aKey");
if (index != -1)
    drawOrder.splice(index, 1);

Splice:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
indexOf:
http://www.w3schools.com/jsref/jsref_indexof_array.asp

Magical Gordon
  • 274
  • 1
  • 8
0

I'm not 100% this will answer your question cause is not clear at least to me.

If you want to remove the whole element but you are worried about founding the right index before actually splice the array you should use Array.indexOF

See this code below:

var data  = {
            'fnc':function(){ 
                      updatePosition(spriteID); 
                      drawSprite(spriteID); 
             },
             'type':'aabb'
            };

var drawOrder= [];
drawOrder.push(data);
console.log(drawOrder);
drawOrder.splice(drawOrder.indexOf(data), 1);
console.log(drawOrder);

As the documentation reports:

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Stefano Saitta
  • 1,486
  • 16
  • 29