7

So we know the code below works:

  $http({ method: 'POST', url: url_getMoreHouse })
        .success(function (data) {
                    alert('Works');
                    console.log(data);

                       $scope.HouseBasket = data;
                });

However if we want to append the data into the current basket which is:

         $scope.HouseBasket += data;

This will cause errors, I dont want to use a foreach loop to push() each data into the $scope.HouseBasket is there a faster way to add a list of Object into the angular list?

3
  • 1
    what is slow about looping and pushing... devpro.it/examples/loopsbench Commented Sep 14, 2014 at 21:59
  • 1
    In your case you just need to concat why even loop though.. Commented Sep 14, 2014 at 22:04
  • Using a faster method instead of a loop that is what I looked for and Commented Sep 17, 2014 at 6:10

1 Answer 1

14

is there a faster way to add a list of Object into the angular list

How big is your data ?, Well a simple for loop will not be slower that most possible solutions, but if your destination list is big enough you could use a while loop progressively popping(or shifting) out of the array and pushing to the destination.

$scope.HouseBasket += data; This will cause errors

You are looking for array.concat $scope.HouseBasket = $scope.HouseBasket.concat(data);


If you want to achieve this in one line you could :-

use function.apply to push a list at once to the source.

     [].push.apply($scope.HouseBasket, data);

or

     $scope.HouseBasket = $scope.HouseBasket.concat(data);
Sign up to request clarification or add additional context in comments.

Comments

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.