0

I have an array that needs to be populated without duplicate objects. However, I am constantly being sent back an array with the duplicate values. Can anyone tell me what I'm doing wrong here and how to fix it?

let holderArr = [
{
    one: "A",
    two: 2,
    three: "String"
},
{
    one: "B",
    two: 2,
    three: "String"
},
{
    one: "A",
    two: 2,
    three: "String"
}]

function removeDuplicates(array){
    let filteredArr = [];
    return duplicateArray.filter((duplicate) => {
        let id = [duplicate.one, duplicate.two].join("|");
        if(filteredArr.indexOf(id) === -1){
            filteredArr.push(id);
        }
        return out;
    })
}
removeDuplicates(holderArr);
Robert
  • 624
  • 2
  • 8
  • 19

2 Answers2

0

am not a professional, but this way it would work.

let holderArr = [1,3,5];

function removeDuplicates(array){
    var filteredArr = [];
for(let i=0; i<array.length; i++){
  if(filteredArr.indexOf(array[i]) === -1){
            filteredArr.push(array[i]);
        }
    }
    return filteredArr;
}
removeDuplicates(holderArr);

I took a look on your code. you have mistaken in somethings. it will work after these edits, try to figure out where did u get it wrong.

function removeDuplicates(array){
  let filteredArr = [];
  return array.filter((id) => {
      if(filteredArr.indexOf(array[id]) === -1){
          filteredArr.push(array[id]);
      }
      return filteredArr;
  })
}
removeDuplicates(holderArr);
Sultan H.
  • 2,908
  • 2
  • 11
  • 23
-1

Looks like you are referring to an unknown variable duplicateArray, try:

function removeDuplicates(array){
  return array.filter(function(item, pos, self) {
    return self.indexOf(item) == pos;
  })
}

let uniqueArray = removeDuplicates(holderArr);

This will populate uniqueArray with what you want.

Phillip Thomas
  • 1,450
  • 11
  • 21