0

Please How can i retrieve the index of these array, mostly when i loop through it by

var objRecord = {"ID":1,"Name":"Precious","PhoneNo":08051170615,"Address":"London"}
var arrData = [objRecord];

arrData.push({ "ID": 2, "Name": "locas", "PhoneNo": 08051109615, "Address": "gameondon"}) 

my aim is to get the index of the second element with ID 2

using these function

function  GetElementIndex(id) {

        //code  

}
  • 3
    `var index = arrData.push(...) - 1` or `var index = arrData.findIndex(a => a.ID === 2)` – Ele Apr 03 '18 at 14:09

4 Answers4

0
function GetElementIndex(id, arr){
   let index = 0
   arr.forEach((item, i) => {
     if(item.ID === id){
       return index += i
     }
   })
  return index
}

that should do the trick

André Gomes
  • 142
  • 1
  • 6
  • That returns a list of objects, not the index. – str Apr 03 '18 at 14:12
  • @str like this work perfectly – André Gomes Apr 03 '18 at 14:22
  • No please do not do that, it is terrible. – str Apr 03 '18 at 14:25
  • @str prefer that way mister ? – André Gomes Apr 03 '18 at 14:32
  • Better but still bad. There is no need to increment the `index` variable as `forEach` already provides it. But anyway, you should use `findIndex` instead. It is meant for exactly that use case and has the better performance as it returns as early as possible. – str Apr 03 '18 at 14:36
  • @str ye should have looked at mdn, didn't knew there was this one, but pointing that makes all response irrelevant when there is one method that do it (or not irrelevant but not the best approach) – André Gomes Apr 03 '18 at 14:52
  • Yes. That is why we are encouraged to close questions as duplicate. Older questions tend to have better and more thorough answers on many topics. – str Apr 03 '18 at 14:58
  • 1
    @str i got it, thanks for pointing that ^^ makes perfect sense ! – André Gomes Apr 03 '18 at 14:59
0
function GetElementIndex(id) {
    var returnIndex = null;
    for (var i = 0; i < arrData.length; i++) {
        if (arrData[i].ID == id) {
            returnIndex = i
            break;
        }
    }
    return returnIndex;
}
NachoDawg
  • 1,533
  • 11
  • 21
0

You can use forEach of Array.prototype.

var objRecord = {"ID":1,"Name":"Precious","PhoneNo":08051170615,"Address":"London"}
var arrData = [objRecord];

arrData.push({ "ID": 2, "Name": "locas", "PhoneNo": 08051109615, "Address": "gameondon"})

function  GetElementIndex(id) {
    var index;
    arrData.forEach(function(e,i){
      if(e.ID===id && !index)
        index=i;
    });
    return index;
}

console.log(GetElementIndex(2));
yajiv
  • 2,901
  • 2
  • 15
  • 25
-1

Here is a simple code that will return the index of the item you want :

var objRecord = {"ID":1,"Name":"Precious","PhoneNo":08051170615,"Address":"London"}
var arrData = [objRecord];

arrData.push({ "ID": 2, "Name": "locas", "PhoneNo": 08051109615, "Address": "gameondon"}) 

var found = arrData.find(e => {
  return e.ID == 2;
})

console.log(arrData.indexOf(found))

Here the idea is to use the array.prototype.find() method to get the element corresponding to your ID, then using indexOf to find the index in the array (here it is 1 because first index starts at 0).

Rafik Tighilt
  • 2,071
  • 1
  • 15
  • 27