var urls = [];
results.forEach(function(result){
posts.forEach(function(post){
post.attachments.every(function(attachment){
if(result === attachment.id) {
urls.push({
id: attachment.id,
full: attachment.images.full.url,
thumbnail: attachment.images.thumbnail.url
});
return false;
}
return true;
});
});
});
A lot of nested foreach loops here, which you should always keep to a minimum, but it all boils down to how big your data is and will be in the future.
the forEach
is a native javascript function and imo makes it way more readable then traditional for loops. The every
method lets you break the loop by returning false so you don't have to traverse the whole list unless matching on the last element in the array.