0

I have an array like this

$scope.dogs = [
 { id: 1, breed: 'German Shepherd' }, 
 { id: 2, breed: 'Collie' }
]

And a second array like this:

$scope.owners = [
 { name: 'Mary', breedowned: 'German Shepherd' }, 
 { name: 'Bill', breedowned: 'German Shepherd' }, 
 { name: 'Bob', breedowned: 'Collie' }
]

I want to push the list of owners into the list of dogs like so basically creating:

$scope.dogs = [
 { id: 1, breed: 'German Shepherd', owners: [...] }
]

I tried to use forEach and push the owners into the dogs array, but it does not work.

 angular.forEach($scope.dogs, function (value, key) {
 for (x = 0; x < $scope.owners.length; x++) {
 if ($scope.owners[i].breedowned == value.breed) {
 $scope.dogs[key].owners.push($scope.owners[i])
 }
 }
 });

Thanks for any help!

jherax
5,2775 gold badges40 silver badges50 bronze badges
asked Aug 25, 2016 at 20:06

3 Answers 3

1

If you don't want any form of dependency, just use Array.prototype.push.apply, this way:

Array.prototype.push.apply($scope.owners, $scope.dogs);
answered Aug 25, 2016 at 20:10
Sign up to request clarification or add additional context in comments.

2 Comments

Don't think that works though, because that would merge the first array into the second. The asker wants the owners pushed to an owners array on the dog itself.
that's why i specified "If you don't want any form of dependency". The question was not clear at 100%.
0

You didnt mention any errors, but I see an issue with you missing var in front of the x in the for loop, and also owners is not initialized in the dog object. Here's a consistent nested loop solution:

angular.forEach($scope.dogs, function (dog) {
 angular.forEach($scope.owners, function (owner) {
 if (owner.breedowned == dog.breed) {
 dog.owners = dog.owners || []
 dog.owners.push(owner)
 }
 })
})
answered Aug 25, 2016 at 20:13

2 Comments

The problem is that I receive the dogs array from a call, so it is not initialized. If initialized, my original attempt works, but thanks. I'm going to try your solution.
Yep that's perfectly okay, just explaining why it likely didnt work :)
0

Here a better solution that only goes through the owners array once and only through the dogs array once.

var tracker = $scope.owners.reduce(function(trackerObj, owner){
 var breedowned = owner.breedowned;
 trackerObj[breedowned] = trackerObj[breedowned] || [];
 trackerObj[breedowned].push(owner);
 return trackerObj;
}, {});
$scope.dogs.forEach(function(dog){
 dog.owners = tracker[dog.breed];
});
answered Aug 25, 2016 at 20:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.