我该如何合并对象的数组? [英] How can I merge an array of objects?

查看:111
本文介绍了我该如何合并对象的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我的文章的阵列,每个项目可能会或可能不会超过1图像对象,现在因为MySQL不能组合在一起的对象,你必须自己做。所以结果是你得到的附近复制文章对象...这里唯一的区别是图像对象。的的重复我的意思是在返回的结果,唯一的区别是图像对象。

我试图找到一种方法,通过文章数组环和elimate重复的对象,并创建一个图片阵列将举行各文章图像对象。

对于给定的下面阵列的物品例如:

  {文章:
   {文章:{条款ArticleID:10,articleText:'胡说'},图像:{条款ArticleID:10,图片网址:'HTTP:// differentUrlTooBelow'}},
   {文章:{条款ArticleID:10,articleText:'胡说'},图像:{条款ArticleID:10,图片网址:'HTTP:// AnotherUrlDifferentToAbove'}},
   {文章:{条款ArticleID:11,articleText:ajsdnfa'},图像:{条款ArticleID:11,图片网址:'HTTP:// DifferentUrlTooBelow'}},
   {文章:{条款ArticleID:11,articleText:asfdjnasjdf'},图像:{条款ArticleID:11,图片网址:'HTTP:// AnotherUrlDifferentToAbove'}},
]}

我们可以使用lodash减少功能,但它似乎只返回最后一个文章(条款ArticleID:11) ...这似乎并没有找到所有它的图像无论是。它只是将创建每篇文章内的图像阵列。请注意, RES 文章对象的数组(如上所示)

  VAR Z = _.reduce(资源,功能(结果,值,键){
如果(键== 0)回报;
如果(value.article ['条款ArticleID'] == RES [KEY-1] .article ['条款ArticleID']){
  结果=值;
  result.images = [];
  result.images.push(result.image);
}返回结果;
},{});

最终的结果会是这个样子:

  {文章:
   {文章:{条款ArticleID:10,articleText:'胡说'},
    图片:
       {条款ArticleID:10,图片网址:'HTTP:// differentUrlTooBelow},
       {条款ArticleID:10,图片网址:'HTTP:// AnotherUrlDifferentToAbove'}
   ]},
   {文章:{条款ArticleID:11,articleText:'胡说'},
    图片:
       {条款ArticleID:11,图片网址:'HTTP:// differentUrlTooBelow},
       {条款ArticleID:11,图片网址:'HTTP:// AnotherUrlDifferentToAbove'}
   ]},
]}


解决方案

您可以使用的_。GROUPBY()来匹配的文章。条款ArticleID ,那么 _。图()来使用的第一个项目每篇文章关联,然后设置图片通过获取所有的图像属性通过项目 _。图()

\r
\r

VAR数据= {\r
  文章:\r
   {\r
     文章:{条款ArticleID:10,articleText:'胡说'},\r
     图片:{条款ArticleID:10,图片网址:'HTTP:// differentUrlTooBelow'}\r
   },\r
   {\r
     文章:{条款ArticleID:10,articleText:'胡说'},\r
     图片:{条款ArticleID:10,图片网址:'HTTP:// AnotherUrlDifferentToAbove'}\r
   },\r
   {\r
     文章:{条款ArticleID:11,articleText:ajsdnfa},\r
     图片:{条款ArticleID:11,图片网址:'HTTP:// DifferentUrlTooBelow'}\r
   },\r
   {\r
     文章:{条款ArticleID:11,articleText:asfdjnasjdf},\r
     图片:{条款ArticleID:11,图片网址:'HTTP:// AnotherUrlDifferentToAbove'}\r
   }\r
  ]\r
};\r
\r
VAR的结果= {\r
  文章:_(data.articles)\r
    .groupBy('article.articleId')\r
    .MAP(功能(项){\r
      返回_(项目[0])\r
        .omit(图像)\r
        .SET('形象',_.map(物品,图像))\r
        。值();\r
    })\r
    。值()\r
};\r
\r
的document.write('< pre>'+ JSON.stringify(结果,0,4)+'< / pre>');

\r

&LT;脚本src=\"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js\"></script>\r
\r
\r

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'} 
   ]},
]}

解决方案

You can make use of _.groupBy() to match article.articleId, then _.map() to associate each article using the first item and then set the images property by getting all image items through _.map().

var data = { 
  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' } 
   }
  ]
};

var result = {
  articles: _(data.articles)
    .groupBy('article.articleId')
    .map(function(items) {
      return _(items[0])
        .omit('image')
        .set('images', _.map(items, 'image'))
        .value();
    })
    .value()
};

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>

这篇关于我该如何合并对象的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆