skip to Main Content

This is my array of objects
i need to filter this with name

case one : if i search ‘tali’ filter result from startwith case, same time user enter ‘tali Co OP’ get the result ‘Taliparamba Co Op Hospital’

case two : if i search ‘ath’ get result of ‘Athikkal Saw Mill,Kallai’ and ‘Marhaba Ice Plant Atholi’ beacause Ath contain in ‘Marhaba Ice Plant Atholi’

 test = [{ id: 1, name: 'Taliparamba Co Op Hospital' },
    { id: 1, name: 'Athikkal Saw Mill,Kallai' },
    { id: 1, name: 'Marhaba Ice Plant Atholi' },]

4

Answers


  1. Chosen as BEST ANSWER
    let filteredArray = [];
            if (event.target.value.length) {
                const regex = new RegExp(event.target.value.split(' ').join('.*'), 'i');
                this.branchListBuffer = this.branchNameList.filter((data) => data.displayName.toLocaleLowerCase().startsWith(event.target.value.toLocaleLowerCase()));
                if (this.branchListBuffer.length == 0) {
                    filteredArray = this.branchNameList.filter(s => regex.test(s.displayName.toLocaleLowerCase()));
                    this.branchListBuffer = filteredArray;
                }`enter code here`
            }
            if (event.target.value.length == 0) {
                this.branchListBuffer = this.branchNameList;
            }
    

    i tried this but some case not get


  2. const test = [
          { id: 1, name: 'Taliparamba Co Op Hospital' },
          { id: 1, name: 'Athikkal Saw Mill,Kallai' },
          { id: 1, name: 'Marhaba Ice Plant Atholi' },
        ];
    
        function search(arr, str) {
          const regexp = new RegExp('\b' + str.split(' ').join('.*?\b'), 'i');
          return arr.filter((item) => regexp.test(item.name));
        }
    
        console.log(search(test, 'tali'));
        console.log(search(test, 'tali Co OP'));
        console.log(search(test, 'ath'));
    Login or Signup to reply.
  3.  const test = [{ id: 1, name: 'Taliparamba Co Op Hospital' },
        { id: 1, name: 'Athikkal Saw Mill,Kallai' },
        { id: 1, name: 'Marhaba Ice Plant Atholi' },]
        
    const myString = "ath"; // your search string goes here
    const result = test.filter(eachObject => {
        return eachObject.name.toLowerCase().search(myString) !== -1
    })
    
    console.log("result is", result)
    Login or Signup to reply.
  4. I think below line of code should do the job for you

    const test = [
        { id: 1, name: 'Taliparamba Co Op Hospital' },
        { id: 2, name: 'Athikkal Saw Mill,Kallai' },
        { id: 3, name: 'Marhaba Ice Plant Atholi' },
    ];
    
    const searchedStr = 'Ath'
    const results = test.filter(obj => 
        obj.name.includes(searchedStr) || 
        obj.name.includes(searchedStr.toLowerCase())
    );
    
    console.log(results)
    

    Please let us know how it goes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search