-1

I am working on an Angular 5 project and I'm trying to filter an array.

I have an array of objects. An object has the property color. I am iterating over my array and I want to get all objects with the same color of my current object in the array. With the code below I seem to get only the first hit in the array and not all the elements.

This is my current code.

for(let object of this.objects){
let a: any = this.objects.find( a => a.color === object.color )
console.log(a);
// Do other stuff
}

Any idea how to solve this?

Thank you.

Catherine
  • 99
  • 9
Simon
  • 925
  • 3
  • 13
  • 30

1 Answers1

2

use the filter instead of find

  let a: any = this.objects.filter( a => a.color === object.color )
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80