IE9: Manipulate a string if the value of the string has a match in an array of Objects

In the below function setChildNumber Strings like ‘Child2’ or ‘Child3’, ‘Child4’ are passed. ‘family’ variable contains array of object. I’ve added a sample data. This code needs to support IE9. So I can’t use ‘find’.

Objective:

I need help finding the index from family array of objects, if family.type === child and id === memberID, the replace memberID with index from family[]

function setChildNumber(member) {
        var family = JSON.parse(sessionStorage.getItem('memberObject'));
        var memberID = +member.match(/d+/)[0];    
        if(!family) return;
        var index = member.toLowerCase().indexOf('child') === 0
          && family.find(function(elem) {
              return (elem.id === memberID);
            }).index;

        return member.slice(0, member.length-1) + ' ' + index;
      }

family = [
    {
        "id": 1,
        "type": "SELF",
        "birthday": "10/10/1980", 
        "age": 40
    },
    {
        "id": 2,
        "type": "CHILD",
        "index": 1,
        "birthday": "10/10/2001"

    },
    {
        "id": 3,
        "type": "SPOUSE",
        "birthday": "10101980",
        "age": 40
    },
    {
        "id": 4,
        "type": "CHILD",
        "index": 2,
        "birthday": "10102010",
        "age": 10
    }
]

Answer

You can use a simple for loop.

var idx = -1;
for(var i = 0; i < family.length; i++){
    if(family[i].type.toLowerCase() === 'child' && family[i].id === memberID){
          idx = i;
          break;
    }
}