2

I have an object array and i am filtering it against property name "username" like this.

array =   [{
    "id": 1,
    "username": "admin",
    "roles": [{
            "name": "Administrator"
        },
        {
            "name": "agent"
        }
    ]
},
{
    "id": 2,
    "username": "admin2",
    "roles": [{
            "name": "Administrator2"
        },
        {
            "name": "agent2"
        }
    ]
},
{
    "id": 3,
    "username": "admin3",
    "roles": [{
            "name": "Administrator3"
        },
        {
            "name": "agent3"
        }
      ]
    }
  ]

and the filter function is like this

  transform(array: any, valueToSearch: string): any[] {

      return array.filter(e => 
       e.username.toLowerCase().indexOf(valueToSearch.toLowerCase()) 
       !== -1);

     }

everything works fine, but now i want to filter against the property name "name" in "roles" array in the object. for example i would like to return an object whose "roles" array contains "name" = agent3 , so it should return the whole object which is located at the last in my example. i tried like

return agents.filter(e => e.roles.filter(ee => 
       ee.valueToSearch.toLowerCase()) !== -1));

but it didn't work.

this is dmeo https://stackblitz.com/edit/angular-txchxs?embed=1&file=src/app/agentFilter.pipe.ts

2

2 Answers 2

2

As per the example given by you in the question, i was able to change your existing function like this and i hope this is your requirement..

  ngOnInit() {
    this.transform(this.array,'agent3');
  }

  transform(array: any, valueToSearch: string): any[] {
    return  this.array.filter(e => {
        e.roles.filter(ee => {
          if(ee.name.toLowerCase() === valueToSearch.toLowerCase() ) {
            console.log(e);
            this.finalResult = e;
          }
        })
      })
  }

Working Stackblitz: https://stackblitz.com/edit/angular-uzgni7

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

22 Comments

your code works fine, but i want a scenario of filter , not search, it only returns object when whole word matches. but i want that when users type a single word it starts to compare and returns suitable value
that scenario i need
@RazaEllahi, Whether this is your expected one?? stackblitz.com/edit/angular-pmzzg7 If you type username then it will filter the data and show the object related to username..
I want to search by "name" attribute in "roles" array
|
0

myarray =   [{
    "id": 1,
    "username": "admin",
    "roles": [{
            "name": "Administrator"
        },
        {
            "name": "agent"
        }
    ]
},
{
    "id": 2,
    "username": "admin2",
    "roles": [{
            "name": "Administrator2"
        },
        {
            "name": "agent2"
        }
    ]
},
{
    "id": 3,
    "username": "admin3",
    "roles": [{
            "name": "Administrator3"
        },
        {
            "name": "agent3"
        }
      ]
    }
  ];
  

function myFunction(){
    var filtered= myarray.filter((obj)=>{
        return obj.username.match(new RegExp(document.getElementById('search').value,'ig'));
    });
    console.log(filtered);
};
<input type="text" id="search" onkeyup="myFunction()"/>

2 Comments

that scenario i need

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.