Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Using the latest AngularJS version (1.3.0-rc.5), I am retrieving a simple JSON object containing people's names. Example of the JSON object:

[{'id':1, 'name':'John'}, {'id':2, 'name':'Jane'}, {'id':3, 'name':'Pete'}]

I cannot figure out how to put all the names into an associative Javascript array. In my mind I want to create an empty Javascript array and loop through the JSON object, and using .push() in the loop to keep adding the names to the array. But I cannot get this working in AngularJS.

ps. I'll accept the answer that helps me out with this.

share|improve this question
    
It looks simple. please create fiddle and show your problem. – Jay Shukla Feb 20 at 10:34
    
1.3.13 is the latest of angular 1.3 and there is also 1.4.0-beta.4 code.angularjs.org – micha Feb 20 at 10:46

4 Answers 4

up vote 4 down vote accepted

Angular is not meant to help you with this, Angular is meant for other parts of the application (UI for instance). If you want to do this, in JS it's simple:

var list = [{'id':1, 'name':'John'}, {'id':2, 'name':'Jane'}, {'id':3, 'name':'Pete'}]
var names = []
for(var idx in list) {
  names.push(list[idx]['name'])
}

You also mentioned an associative array, in JS you have objects, which work similarly, but I don't understand how you want to use them... what is the index for that associative array? The name? If that's the case, then what is the value? is it the ID? If so, you could do something like this:

var list = [{'id':1, 'name':'John'}, {'id':2, 'name':'Jane'}, {'id':3, 'name':'Pete'}]
var names = {} //object literal
for(var idx in list) {
  names[list[idx]['name']] = list[idx]['id']
}

Hope that helps

share|improve this answer

You can do this in pure javascript

var arr = [{'id':1, 'name':'John'}, {'id':2, 'name':'Jane'}, {'id':3, 'name':'Pete'}];
var nameArr =[];
arr.forEach(
function(elem){ 
  nameArr.push(elem.name);
});
share|improve this answer

Use map(..) on your array object.

var obj = [{'id':1, 'name':'John'}, {'id':2, 'name':'Jane'}, {'id':3, 'name':'Pete'}];
var result = obj.map(function (x) {return x["name"]; });
//["John", "Jane", "Pete"]
share|improve this answer
    
Man, map is cross browser already? How did I not know this!? Thanks, you brightened my day! – Deleteman Feb 20 at 10:41
    
I would expect it to work on all modern browsers. I do not know about Internet Explorer, though. – Thrustmaster Feb 20 at 10:46
    
Wait, I take that back. Internet Explorer doesn't fit my definition of modern. :P – Thrustmaster Feb 20 at 10:47
    
Well according to this: kangax.github.io/compat-table/es5/#Array.prototype.map even IE supports it. – Deleteman Feb 20 at 11:26

Use angular.fromJson() function to solve this.

share|improve this answer

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.