0

I have an array of position and nested user objects like so:

[ 
  position: {
    title: Developer,
    user: { name: Albert }
    },
  position: {
    title: CEO,
    user: { name: Smith }
    }
] 

How do I get an array of [user, user] with Angularjs?

6
  • 2
    I think the solution you're after has nothing to do with angular, but with pure vanilla js, Commented Apr 25, 2015 at 10:40
  • I've tagged it with angular because that's the framework I'm using. A vanilla js solution would suit me fine. Commented Apr 25, 2015 at 10:41
  • 1
    @gion_13 It could have done. Angular has angular.forEach which polyfills Array.forEach. That could easily have been pertinent to the solution. Commented Apr 25, 2015 at 10:43
  • You should first fix the issue in your code snippet, because it is missing a few { and also }. from what I can see it doesn't seem to be an array, instead it sounds like a single object. But if it really is an array of positions it should be like: [{title:'A', user:{}},{title:'B', user:{}}] or at least: [{position:{title:'A', user:{}}}, {position:{title:'B', user:{}}}] Commented Apr 25, 2015 at 10:46
  • You're right @MehranHatami, I forgot to mention I was using coffeescript. Commented Apr 25, 2015 at 10:52

4 Answers 4

3

Assuming this is pseudo-code (as it's missing a few { and } and you'd need variables called Developer, Albert, CEO & Smith - I've fixed these in the working example) you can use Array.map:

var arr = [{ 
  Position0: {
    title: Developer,
    user: { name: Albert }
    },
 {
  Position1 : {
    title: CEO,
    user: { name: Smith }
    }
}];
var result = arr.map(function(val, idx) {
    return val["Position" + idx].user.name;
});

Working Example

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

1 Comment

Just a note for other newbies like me, | val["Position" + idx].user.name; | returns an array of values, | val["Position" + idx].user; | returns an array of objects.
2

I just tried this code, and works ...

var positions, users;

users = [];
positions = [
  {
    title: 'Developer',
    user: { name: 'Albert' }
  },
  {
    title: 'CEO',
    user: { name: 'Smith' }
  }
];


angular.forEach(positions, function(position) {
  users.push(position.user);
});

console.log(users);

Comments

1

Firstly this thing does not need angularjs.

The way you can do that is using for each loop:

var users = [];
var positions = [{ 
  Position0: {
    title: 'Developer',
    user: { name: 'Albert' }
    },

  Position1 : {
    title: 'CEO',
    user: { name: 'Smith' }
    }
}] 
    for(var i =0; i< positions.length; i++){
        for(var j in positions[i]){ //used to loop on object properties
          if(positions[i].hasOwnProperty(j)){ //checking if the property is not inherited
             if(positions[i][j].user){ //checking if the property exist, then only will push it to array, so that array doesnot have undefined values
                users.push(positions[i][j].user);
             }
          }
        }
    }

users array will have the users.

Comments

1

Use angular.forEach():

var arr1=[]
var arr=[ 
  position: {
    title: Developer,
    user: { name: Albert }
    },
  position: {
    title: CEO,
    user: { name: Smith }
    }
];
angular.forEach(arr,function(value,key){
   arr1[key]=value.position.user;
});
console.log(arr1);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.