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);
        }) }
2

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.