0

In the function below, I am trying to build the filteredProducts array by concatenating the value of the tmp array. The tmp array contains elements but after the concatenation, the filteredProducts array is empty.

Assume the boolean variable refineCriteria.bosch is true;

filterProducts(refineCriteria: Refine) {
this.filteredProducts = [];
let tmp = [];

let filtered = false;

if(refineCriteria.bosch) {
  tmp = this.products.filter(product => product.brand == "Bosch");
  this.filteredProducts.concat(tmp);
  filtered = true;      

  console.log("tmp = " + tmp);
  console.log("this.filteredProducts = " + this.filteredProducts);

}


if(!filtered) {
  this.filteredProducts = this.products;
}

console.log("filteredProducts = " + this.filteredProducts); 

this.filteredProductsEvent.emit(this.filteredProducts);
}
5
  • 3
    this.filteredProducts = this.filteredProducts.concat(tmp) Commented Jul 31, 2017 at 15:49
  • you need to assign it back to a variable this.filteredProducts = this.filteredProducts.concat(tmp) Commented Jul 31, 2017 at 15:50
  • developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Jul 31, 2017 at 15:51
  • Worked beautifully. Thanks a bunch. Commented Jul 31, 2017 at 15:51
  • 2
    Marked it as duplicate, since it's not really an Angular question and comments have already answered it Commented Jul 31, 2017 at 15:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.