Reputation: 9570
I have an array listOfFriends
of objects that contain [{"name" : "fName lName", "uid" : "0102030405"}], ...
I want to cut down the size of this array , since I only need the uids
, so I would like to make another array with only the uids
var listOfFriends, listOfFriendsUIDs;
//listOfFriends gets filled
for(var i = 0; i < listOfFriends.length; i++){
listOfFriendsUIDs[i].uid = listOfFriends[i].uid; // this line isn't working
//listOfFriendsUIDs is still undefined untill the start of this loop
}
Upvotes: 0
Views: 63
Reputation: 145388
You can use Array.prototype.map
to create new array out of IDs:
var listOfFriendsUIDs = listOfFriends.map(function(obj) { return obj.uid; });
Check the browser compatibility and use shim if needed.
DEMO: http://jsfiddle.net/x3UkJ/
Upvotes: 4
Reputation: 1320
Try to use projection method .map()
var listOfFriendsUIDs = listOfFriends.map(function(f){ return f.uid;});
Upvotes: 1
Reputation: 115940
listOfFriendsUIDs[i]
is not defined, so you can't access its uid
property. You either need:
// creates an array of UID strings: ["0102030405", ""0102030405", ...]
listOfFriendsUIDs[i] = listOfFriends[i].uid;
or
// creates an array of objects with a uid property: [{uid:"0102030405"}, ...]
listOfFriendsUIDs[i] = {}
listOfFriendsUIDs[i].uid = listOfFriends[i].uid;
Upvotes: 0