2

I've got 2 data sets

posts

[
 {
  "postId": 1,
  "postContent": "Lorem Ipsum",
  "user": 1
 },
 {
  "postId": 2,
  "postContent": "Lorem Ipsum",
  "user": 2
 },
]

and users

[
 {
  "user": 1,
  "firstName": "John",
  "lastName": "Doe"
 },
 {
  "user": 2,
  "firstName": "Johny",
  "lastName": "Doey"
 }
]

I am repeating all the posts and i would like to access a user data throught the $scope.post.user.firstName etc.

how should i aproach it?

7
  • What have you tryed thus far? Commented Dec 2, 2016 at 16:08
  • What is your desire output? Commented Dec 2, 2016 at 16:10
  • There is a little typo in the users data set btw Commented Dec 2, 2016 at 16:11
  • @MathieudeLorimier i am using $resource directive and i've tried to do something like $scope.post.user = user.get({id: $post.user}) also i have found out about .merge() and .extend() functions via google search, but i've decided to ask before implementig one of them Commented Dec 2, 2016 at 16:13
  • @MasoodRehman to access user data in a way that is like accessing nested json Commented Dec 2, 2016 at 16:15

3 Answers 3

2

You can implement a method like getUserById() with the use of Array.prototype.find() to use it when iterating over your posts.

Code in AngularJS:

angular
  .module('App', [])
  .controller('AppController', ['$scope', function ($scope) {
    $scope.posts = [{"postId": 1,"postContent": "Lorem Ipsum","user": 1},{"postId": 2,"postContent": "Lorem Ipsum","user": 2}];
    $scope.users = [{"user": 1,  "firstName": "John",  "lastName": "Doe"},{  "user": 2,  "firstName": "Johny",  "lastName": "Doey"}];

    $scope.getUserById = function (id) {
      return $scope.users.find(function (user) {
        return id === user.user;
      });
    };
  }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>

<div ng-app="App" ng-controller="AppController" class="row">
  <div ng-repeat="p in posts">
    <p>{{p.postContent}} <i>by {{getUserById(p.user).firstName}}</i></p>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

2

find() the matching user for each post.

Note that if you are not using ES6 features, you will need a polyfill for Object.assign() and should use regular function() {}s instead array functions.

var posts = [{"postId":1,"postContent":"LoremIpsum","user":1},{"postId":2,"postContent":"LoremIpsum","user":2}];
var users = [{"user":1,"firstName":"John","lastName":"Doe"},{"user":2,"firstName":"Johny","lastName":"Doey"}];

var out = posts.map(post => Object.assign({}, post, {
  user: users.find(user => user.user === post.user)
}));
                
console.log(out);

Comments

0

You can create a function that maps each user object to your post object. Then you can access user details as $scope.post.user.first_name; Here is simple code that will make you an new object with users.

var new_object = [];
function mapUsersToPosts(users, posts){
   angular.forEach(posts, function(post){
      angular.forEach(users, function(user){
         if(post.user_id == user.user_id){
            new_object.push({
                'post' : post,
                'user' : user
            });
         }
      });
   });
}

Now you can loop new_object and use user data and post data in same object.

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.