suppose have array of objects called myarray
, function returns reference particular element within array; this:
myarray = [object1, object2, ..., objectn]; function dowork() { var theobject = gettheobject(someparamter); }
at point, theobject
points element in array. suppose want remove element myarray, possible without having reloop through array index of element? i'm looking splice work reference element rather index of element.
thanks.
simply use array.prototype.indexof
:
let index = myarray.indexof(theobject); if(index !== -1) { myarray.splice(index, 1); }
keep in mind if targeting ie < 9 need introduce polyfill indexof
; can find one in mdn page.
Comments
Post a Comment