How do I retrieve the index of the array that is passed? The solution I currently use is sending the index too, but that doesn't feel right.
var obj = {arr: [{x: 1, y: 2},{x: 3, y: 4},{x: 5, y: 6}]};
function myFunction(myObj)
{
alert(myObj); // 5
// alert(the index of the array that is passed); // 2
}
myFunction(obj.arr[2].x);
You can use Array.indexOf
jsfiddle
var obj = {arr: [{x: 1, y: 2},{x: 3, y: 4},{x: 5, y: 6}]};
function myFunction(myObj)
{
alert(obj.arr.indexOf(myObj));
}
myFunction(obj.arr[2]);