2

I want to filter an array of object with multiple array string values.

below is my array object is like

[{
    "name": "FULLY MAINTAINED MARUTI SUZUKI SWIFT VDI 2008",
    "model": "Swift"
}, {
    "name": "maruti suzuki test",
    "model": "Swift Desire"
}, {
    "name": "maruti suzuki test2",
    "model": "Alto"
}, {
    "name": "maruti suzuki swift desire for sale1",
    "model": "Zen"
}, {
    "name": "maruti suzuki test",
    "model": "Alto"
}, {
    "name": "maruti suzuki test",
    "model": "Zen"
}]

I am filtering this array of objects (above) with an array of strings (below)

this.filterData.model=["Swift","Zen"]

it has has to be display with all Swift and Zen models. I have do with single value how to find with array of values.

Added a plunker link stackblitz Editor link

2 Answers 2

1

I tested this in your stackblitz it's not in edit mode :(.

Here is the code I added and extra variable on the component scope this.filteredData:any[] = [];

this.filterData.model=["Swift", "zen"];

 for(let i = 0; i < this.filterData.model.length; i++){
   let products = this.products.filter((product: any) =>
   product.model.toLowerCase().indexOf(this.filterData.model[i].toLowerCase()) === 0);
   for(let j = 0; j < products.length; j++){
      this.filteredData.push(products[j]);
   }
 }
 console.log(this.filteredData);
Sign up to request clarification or add additional context in comments.

Comments

1

All you need is just change this line from filter.pipe.ts

.find(key => item[key] !== filter[key]); // this was for string comparison

To :

.find(key => filter[key].indexOf(item[key]) == -1); // this is how you can do that with array

WORKING DEMO

2 Comments

if we add an another string to array it is not updating
I Used @Swoox logic in pipe its worked for me,.. thank you for your efforts.

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.