-2

I'm trying to pass a parameter to a function so that I can call my array object elements multiple times (since I have them quite a lot, returned from the sql query).

removeDuplicates(arr, x){
    var tmp = [];
    var tmp2=[];
    for(let i = 0; i < 50; i++){
        if(tmp.indexOf(arr[i].id) == -1){
        tmp.push(arr[i].id); //always id
        tmp2.push(arr[i].x);  //for example arr[i].name
        }
    }
    return tmp2;
}

If I do it like that and call the method like this:

 removeDuplicates(arraytag2, arraytag2.name); 

it doesn't work.

EugenSunic
  • 13,162
  • 13
  • 64
  • 86

1 Answers1

1

Why don't you use the amazing filter function in arrays?

You then can make your own validation inside the function item by item and it's own properties.

function uniq(a) {
    var seen = {};
    return a.filter(function(item) {
        return seen.hasOwnProperty(item) ? false : (seen[item] = true);
    });
}

var arr = ["hello", "bye", "hola", "adios", "see you", "hello", "hola"];
var uniqArr = uniq(arr);

console.log(arr);
console.log(uniqArr);
Jordi Flores
  • 2,080
  • 10
  • 16