-1

I have this:

BasesFunctions.AllAgents = function(self){
    self.agents = self.datas.reduce(function (all, item, index) {
        item[self.valORname] == self.datas.valORname;
        all.push(item[self.valORname]);
        return all;
    }, []);
};

My problem is simple this function return:

Array [ "BLABLA", "TEST", "HONORE", "SPONGEBOB", "PATRIC", "AVEA", "TEST", "HONORE", "PATRIC", "TEST", 16 more… ]

I want when my value exist to not push in my array, just a different value for index 1,2,3 ... . I don't want duplicate my value like that [ "BLABLA", "TEST", "HONORE", "SPONGEBOB", "PATRIC", "AVEA", "TEST", "HONORE", "PATRIC", "TEST", 16 more… ]

Can you help me ? plz Thanks

Thanks all i have the answer ^^ is very simple i just need to use IndexOf() like this :

BasesFunctions.AllAgents = function(self){
    self.agents = self.datas.reduce(function (all, item, index) {
        item[self.valORname] == self.datas.valORname;
        if(all.indexOf(item[self.valORname]) === -1)
        {
        all.push(item[self.valORname]);
        }
        return all;
    }, []);
};
B.Denis
  • 23
  • 8
  • possible duplicate of http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – Suchit kumar Mar 02 '17 at 08:29
  • what is the benefit from this line: `item[self.valORname] == self.datas.valORname;` ?! – RomanPerekhrest Mar 02 '17 at 08:29
  • Please check this method at http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items – Cristi Marian Mar 02 '17 at 08:31
  • this line is just for select my item its item.lastname because valORname = "lastname" on my js. Ok thanks for your answers – B.Denis Mar 02 '17 at 08:31
  • Possible duplicate of [Remove Duplicates from JavaScript Array](http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array) – afxentios Mar 02 '17 at 09:41

2 Answers2

0

You can extend the prototype of the array like:

// check if an element exists in array using a comparison function
// comparer : function(currentElement)
Array.prototype.inArray = function(comparer) { 
    for(var i=0; i < this.length; i++) { 
        if(comparer(this[i])) return true; 
    }
    return false; 
}; 

// adds an element to the array if it does not already exist using a comparer 
// function
Array.prototype.pushIfNotExist = function(element, comparer) { 
    if (!this.inArray(comparer)) {
        this.push(element);
    }
}; 

Now use:

var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist(element, function(e) { 
    return e.name === element.name && e.text === element.text; 
});
Shakti Phartiyal
  • 5,370
  • 2
  • 19
  • 42
0

Try using filter function on Array:

 var arr =  [ "BLABLA", "TEST", "HONORE", "SPONGEBOB", "PATRIC", "AVEA", "TEST", "HONORE", "PATRIC", "TEST"];

var res = arr.filter((item, index, array) => array.indexOf(item) === index);

res.forEach(el => console.log(el)) 
Ali Ezzat Odeh
  • 1,983
  • 1
  • 14
  • 17
Sekar
  • 357
  • 3
  • 19