How can I filter an array of objects based on certain conditions when I am unable to access the array data within the if condition? Any suggestions on how to approach this issue?
data =[
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
},
{
color: "cyan",
value: "#0ff"
},
{
color: "magenta",
value: "#f0f"
},
{
color: "black",
value: "#000"
}
]
selection: string = "";
onClick($event: any) {
this.selection = $event.value
}
filterArray(): void {
let myData = this.data
if (this.selection != null) {
let filteredData = myData.filter(x => x.color == "black" && x.value == "#000");
console.log(filteredData) // this is not logging
}
else {
console.log(myData) this also is not logging
}
}