Say I had an array of articles, each article may or may not have more than 1 image object, now since mysql can't group together objects, you have to do it yourself. So the result is you getting near duplicate article objects...where the only difference is the image object. By near duplicate i mean the only difference in the returned result is the image object.
I'm trying to find a way to loop through the articles array and elimate duplicate objects and create an images array which will hold each article image objects.
For example given the below array of articles:
{ articles: [
{article: {articleId: 10, articleText: 'blah'}, image: {articleId: 10, imageUrl: 'http://differentUrlTooBelow'} },
{article: {articleId: 10, articleText: 'blah'}, image: {articleId: 10, imageUrl: 'http://AnotherUrlDifferentToAbove'} },
{article: {articleId: 11, articleText: 'ajsdnfa'}, image: {articleId: 11, imageUrl: 'http://DifferentUrlTooBelow'} },
{article: {articleId: 11, articleText: 'asfdjnasjdf'}, image: {articleId: 11, imageUrl: 'http://AnotherUrlDifferentToAbove'} },
]}
We could use the lodash reduce function, but it seems to only return the last article (articleId: 11)...It doesn't seem to find all it's images either. It will just create an images array inside each article. Note that res is the array of article objects (Like shown above)
var z = _.reduce(res, function(result, value, key) {
if (key == 0) return;
if(value.article['articleId'] == res[key-1].article['articleId']) {
result = value;
result.images = [];
result.images.push(result.image);
}
return result;
}, {});
The end result would look something like this:
{ articles: [
{article: {articleId: 10, articleText: 'blah'},
images: [
{articleId: 10, imageUrl: 'http://differentUrlTooBelow'},
{articleId: 10, imageUrl: 'http://AnotherUrlDifferentToAbove'}
]},
{article: {articleId: 11, articleText: 'blah'},
images: [
{articleId: 11, imageUrl: 'http://differentUrlTooBelow'},
{articleId: 11, imageUrl: 'http://AnotherUrlDifferentToAbove'}
]},
]}
groupBy.articleIdin the image object. Seems like redundant data since its already part of the parentarticleobject. And if the only data coming from your images is the url than you could just use a group_concat in your mysql query and then split it later when you need to actually use the urls