2

I'm using angularJS and I have a problem: I have 2 $scope:

$scope.list1{
Object 1,
Object 2,
Object 3}

$scope.list2{
Object 4,
Object 5,
}

and I want this result:

$scope.res{
Object 1,
Object 2,
Object 3,
Object 4,
Object 5}

I don't know what function to use in agularJS, I did use

angular.merge($scope.res, $scope.list1, $scope.list2)

but I got empty result I don't know if merge is the good function so I need your help

3
  • 2
    please add valid data. Commented May 3, 2017 at 15:01
  • 1
    What nina is saying is that there is nothing in javascript with that syntax. There are arrays $scope.list = [obj1, obj2, obj3]; and there are objects $scope.list1 = { key: obj1, key: obj2 };. You are saying list, so arrays perhaps? But you are using curly brackets, so probably objects then? But there are no key/values so perhaps arrays anyway? As the question reads right now the answer you need is: Go and read some basic javascript tutorial that explains arrays and objects, and then please come back and try again! (Or you could edit your question and add valid data.) Commented May 3, 2017 at 15:21
  • look at this: stackoverflow.com/questions/43759566/… this is my problem, I didn't get any answer so I write a simple post here :) Commented May 3, 2017 at 16:30

3 Answers 3

4

The Angular way would be using angular.extend:

Extends the destination object dst by copying own enumerable properties from the src object(s) to dst. You can specify multiple src objects. If you want to preserve original objects, you can do so by passing an empty object as the target: var object = angular.extend({}, object1, object2).

angular.extend($scope.list1, $scope.list2);
Sign up to request clarification or add additional context in comments.

Comments

2

You can use array concat.

Ex:

$scope.res = $scope.list1.concat($scope.list2);

1 Comment

That's nice! ;)
0
$scope.list1.forEach(function (selectedItem) {
        $scope.res.push(selectedItem);
    });
$scope.list2.forEach(function (selectedItem) {
        $scope.res.push(selectedItem);
    });

Try above code

3 Comments

Can you please let us know if the solutions given here worked for you?
Can you please explain why he should try the above? How does this solve his problem and what more does this offer compared to the other answers?
I knew this answer, I never tried other options... You are too rude

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.