1

I have two arrays, one store the conditions and other array contain data objects. I am looking for a way to iterate through the condition array and if anyone match found, then add them to the result. Below is the function, that I wrote to do this but with always with only one condition i.e. searchableColumns[0]

private _filter(value: any): string[] {
    const filterValue = value.toLocaleLowerCase();
    var result = this.searchData.filter(option => option[this.searchableColumns[0]].toLocaleLowerCase().includes(filterValue));
    return result;
  }

But I want to check for other searchableColumns as well. These matching is an OR condition. Also When adding to the result, I want to concatenate the object's property as string.

For, example, say I have

this.searchData = [{key: "1", name: "One", value: "Once"}, {key: "2", name: "Two", value:"On Twice"}, {key: "3", name: "Three", value: "Thrice"}];

and

this.searchableColumns = ["name", "value"];

and when the function is called with parameter On, then the result will be

result = ["1|One|Once", "2|Two|On Twice"] 

as result[0] has name with "On" and result[1] contains value with "On".

Please let me know, if it can be done and can be improvised. Thank you for your time.

2 Answers 2

1
private _filter(value: any): string[] {
    const filterValue = value.toLocaleLowerCase();
    var result = this.searchData.map(item => {
      if(Object.values(item).filter(ele => ele.contains(filterValue)).length){
         return `${item.key} | ${item.name} | ${item.value}`
    }
    return result;
}

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. I also want to consider the array searchableColumns. I tried to fit in the code and working on it.
0

Yep! You should be able to manipulate this to get the output format you want

filter(filterValue){
        let result = []
        let objs = [{key: "1", name: "one", value:"once"}, {key: "2", name: "Two", value:"on twice"}]
        let searchableColumns = ['name', 'value']
        searchableColumns.forEach(column => {
          result.push(objs.filter( obj => obj[column].includes(filterValue)))
        })
        return result
      }

3 Comments

This page really helped me understand the filter function: medium.com/poka-techblog/…
Thanks for the response. I tried to use the code and the result came up with few unmatched data as well. I am working to fit in the code.
Also to consider the OR condition, Array#some developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… was working.

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.