Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

How do I join two scope arrays into one in angularjs in my controller so that I can display them in a single table? The join would happen on a field which is in both tables. It would be like a typical SQL join where you want data from both tables in one table. Thank you in advance.

$scope.stuff = [{desc: "stuff", id: "1234"},{desc: "things", id: "1235"}]

$scope.otherStuff = [{type: "new", id: "1234"},{type: "old", id: "1235"}]

$scope.result (how do I get this) = [{desc: "stuff", type: "new", id:"1234"},{desc: "things", type: "old", id:"1235"} ]
share|improve this question
    
Neither of those is a valid array. – Amy Blankenship 2 days ago
    
Of course...let's assume I have two valid arrays and want to extend one of them with another (via a join on one array property) – sstrick 2 days ago
    
They're both very much valid arrays. You can have an object inside an array. I think the OP wants to merge two objects from separate collections into one object in a new/existing collection. – Seth 2 days ago
    
You need to provide more information for this question to be answerable. Do you want your original objects to stay unaffected? What should happen if there are multiple objects in each source array rather than just one? – Amy Blankenship 2 days ago
    
Yes good clarification question which I should have stated originally. I have two different scope arrays with multiple objects in each of them. They have a common relationship via one field (ID) and I want to create a new scope array which is a joined version of both of them. The new array should include fields from both tables. – sstrick 2 days ago

Use plain javascript:

$scope.result = $scope.stuff.concat($scope.otherStuff)

UPDATE

As Amy said your arrays are invalid.. It seems you want to extend an object with another one:

$scope.stuff = {desc: "stuff", id: "decbcf53-5d44-4d43-b1c4-5c3f83a129f8"};
$scope.otherStuff: = {type: "2", id: "decbcf53-5d44-4d43-b1c4-5c3f83a129f8"};
$scope.result: angular.extend($scope.stuff, $scope.otherStuff);

Notice I use {, } and not [, ] for object and not an array.

share|improve this answer
    
That only works if you start with a valid array... – Amy Blankenship 2 days ago
    
You're right.. it seems he doesn't want to join two arrays but extend an object with another one.. – Adam Tal 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.