For loop using find

by jessekinsman

JavaScript

function deDupeArrByPropForLoop(objectArray, prop) {
  // create a new array that will be returned
  const returnArr = [];
  // Loop through the array passed to the function as first param
  for (let i = 0; i < objectArray.length; i+=1) {
    // Using array.findIndex check if the new array contains same prop value
    if (returnArr.findIndex(item => {return objectArray[i][prop] == item[prop]}) == -1) {
      returnArr.push(Object.assign({},objectArray[i]));
    }
  }
  // return the new array
  return returnArr;
}