0

I have array that i get from service, and in that array I have elements that are same (example: 1,2,2,3,1,1,3). I want to make another array that will contain only one of each elements from previous array (so new array should look like [1,2,3 etc). Can you point me to error in this function. Thanks

this.dataService.get().subscribe( response => {

        this.Data = response;
        response.forEach(res => {
            for (this.i = 0; this.i < this.headData.length; this.i++){
                res.head !== this.headData[this.i];
            }
            this.headData.push(res.head);
        }) }
John Theoden
  • 325
  • 2
  • 10
  • 23

1 Answers1

2

Use the Set to generate unique items in the array

var items = [1,2,2,3,1,1,3]
var uniqueItems = Array.from(new Set(items))

reference

Or you can use sortedUniq Lodash fucntions.

_.sortedUniq([1, 1, 2]);
// this is the result  => [1, 2]
Abel Valdez
  • 2,368
  • 1
  • 16
  • 33