0

Problem : API returning JSON data in below format in my case all CategoryName is returning in Dropdown list.

I want to populate only Unique CategoryName but it's populating all three as per JSON data.(returning 3 time AAA it should be only 1 time )

Note : Looking complete working solution because I tried in many ways.

**

  • JSON Data Format:

** [{"catID":200,"categoryName":"AAAA","sID":1,"subCategoryName":"XXX","active":1},{"catID":200,"categoryName":"AAAA","sID":1,"subCategoryName":"XXX","active":1},{"catID":200,"categoryName":"AAAA","sID":1,"subCategoryName":"XXX","active":1},{"catID":201,"categoryName":"BBB","sID":1,"subCategoryName":"YYYY","active":1},{"catID":201,"categoryName":"BBB","sID":1,"subCategoryName":"YYYY","active":1},{"catID":201,"categoryName":"BBB","sID":1,"subCategoryName":"YYYY","active":1},

REST API ->Controller: (URL calling in Frontend to popluatethe JSON data)

@GetMapping("/getAllCategory")
{
        public List<ccCategory> getAllCategory() {
        List<ccCategory> cCategory = categoryRepository.getCategoryName();
        return cCategory;
}

In Angular Side:

service.ts:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ICategory } from '../model/emporvendor/ICategory';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class CategoryService {

  public URL = 'http://localhost:8080/getAllCategory';

  constructor(private http: HttpClient) { }

  getAllCategory(): Observable<ICategory[]> {
    console.log("Call Observable ICategory getAllCategory()");
    return this.http.get<ICategory[]>(this.URL);
  }

}

.component.ts :>

export class NewinquiryComponent implements OnInit {

constructor(private categoryService: CategoryService,private route: ActivatedRoute) { }

ngOnInit() {
    this.categoryService.getAllCategory()
        .subscribe(data => this.listAllCategory = data);

    myMethod();
  }

}


html:

<select  [(ngModel)]="enquirycategory" name="enquirycategory" class="form-control" required>
<option *ngFor="let enquirycategory of listAllCategory" [value]="enquirycategory.catID"> {{enquirycategory.categoryName}}
</option>
    </select>
Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
Jaiswal
  • 3
  • 2
  • 1
    Does this answer your question? [How do I make an array with unique elements (i.e. remove duplicates)?](https://stackoverflow.com/questions/6940103/how-do-i-make-an-array-with-unique-elements-i-e-remove-duplicates) – Igor May 05 '20 at 17:32
  • Possible duplicate of [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/q/1960473/1260204) – Igor May 05 '20 at 17:32

0 Answers0