Hadi Amiri Hadi Amiri - 2 months ago 12x
AngularJS Question

Implementing search in angular with angular.copy

I have a simple button that when clicked, it will filter a list and return the filtered list:

var originalArray = [{name: "A", number: 1},{name: "B", number: 2},....]

And here is the

function filterList(filterName, filterNumber) {
var filteredList = angular.copy(originalArray);

filteredList = filteredList.filter(function(item){
return item.name === name

return filteredList

My question is am I using the right way to implement this feature? suppose that user clicks
button 10000 times ! do I have a 10000 copy of my


As filter returns just an array, use that instead and you won't need to use angular.copy

function filterList(filterName, filterNumber) {
    return originalArray.filter(function(item){
        return item.name === name

That said there are better ways of doing this if you're doing this from a view. Angular already has built in tools for filtering.