I am trying to add a value from one object to another if both have the same value.
This is how the objects looks like basically:
var comments = [
{
author: '4jdf7s',
message: 'Comment text here',
posted: '2014-12-29 14:30'
},
{
author: '87sd3w',
message: 'Comment text here',
posted: '2014-12-30 12:00'
}
];
var users = [
{
_id: '87sd3w',
username: 'MyUsername'
},
{
_id: '4jdf7s',
username: 'OtherUsername'
}
];
And as author and _id is the same, I want to add users.username to comments.username like this:
var comments = [
{
author: '4jdf7s',
username: 'OtherUsername',
message: 'Comment text here',
posted: '2014-12-29 14:30'
},
{
author: '87sd3w',
username: 'MyUsername',
message: 'Comment text here',
posted: '2014-12-30 12:00'
}
];
The comments object has been sorted, which is why it can't be scrambled aswell.
This is my current code but it doesn't work at all:
comments.forEach(function(i, index) {
users.forEach(function(e) {
if(e._id == i.author) {
comments[index].username = e.username;
}
});
});