var myIds = [3, 4, 2];
var myObj = [
{id:1, name:'one'},
{id:2, name:'two'},
{id:3, name:'tree'},
{id:4, name:'four'}];
// need to obtain ['tree', 'four', 'two']
var idsToNames= function(ids, objects) {
var myNames = myIds.map(function(id){
// transform id to name
foreach(o in objects){
if (i.id == id)
return o.name;
}
});
return myNames;
}
Is this the most efficient method for converting an array of ids into an array of corresponding names?