合并使用Angularjs或下划线的js不同值的对象(Merge objects with diff

2019-10-20 14:18发布

我试图通过“UNIQUE_ID”两个对象合并成用于Angularjs控制器使用单一多维对象。 (注意:我也有下划线的js加入)。

对象#1例如:

[ 
  { "unique_id": "001", "title": "Putting Green Challenge - Motion Depends on Force and Mass" }, 
  { "unique_id": "002", "title": "Molecules to Organisms: Frog Life Cycle" }
]

对象#2的例子(具有比MANY对象物1更多的行..):

[
  {

   "ab_id": "76153F02-29F3-11D8-95EA-951BF95D9AEF",
   "unique_id": "001",
   "title": "How Speed Relates to Energy",
   "state": "NY",
   "document_title": "Core Curriculum",
   "grade_code": "K-4",
   "grade_descr": "Elementary",
   "state_id": "1.S2.3a",
   "state_text": "Use appropriate \"inquiry and process skills\" to collect data"

},
{

  "ab_id": "7980A762-29F3-11D8-BD14-861D7EA8D134",
  "unique_id": "001",
  "title": "How Speed Relates to Energy",
  "state": "NY",
  "document_title": "Core Curriculum",
  "grade_code": "5-8",
  "grade_descr": "Intermediate",
  "state_id": "1.S3.2d",
  "state_text": "formulate and defend explanations and conclusions as they relate to scientific phenomena"

}
]

我的控制器:

abApp.controller("abEE", function(abService, $http, $scope, $q, _) {
  var abApp = this;

  $scope.abData = $http.get('/data/ab_activities.json', {
    cache: false
  });
  $scope.eeData = $http.get('/activities/eedata', {
    cache: false
  });

  $q.all([$scope.eeData, $scope.abData]).then(function(values) {
    var val = ??? This is where I want to merge the objects into one big multidimensional object..
});

下面是console.dir(值)的输出端;

0  Object { data=[28], status=200, config={...}, more...}
1  Object { data=[743], status=200, config={...}, more...}

这是所需的输出,我想尝试并获得:

[
  { "unique_id": "001", "title": "Putting Green Challenge - Motion Depends on Force and Mass", "alignments": [{"ab_id": "76153F02-29F3-11D8-95EA-951BF95D9AEF","unique_id": "001","title": "How Speed Relates to Energy",...}, {"ab_id": "7980A762-29F3-11D8-BD14-861D7EA8D134", "unique_id": "001", "title": "How Speed Relates to Energy",...}]
]

Answer 1:

编辑

你更新的问题后,我创造了这个plunker

希望这是你的意思


要合并的所有对象unique_id

var unions = {};

$q.all([$scope.eeData, $scope.abData]).then(function(values) 
    {
        for (var i = 0; i< values.length; i++)
        {
            var value = values[i];


            if (!unions[value.unique_id])
            {
                unions[value.unique_id] = {};
            }

            angular.extend(unions[value.unique_id], value);
        }
    });

// Do somthing with 'unions'
...


Answer 2:

如果你可以切换使用lodash而不是下划线 ,可以实现这样的:

var val = _.values(_.merge(_.indexBy(values[0].data, 'unique_id'), _.indexBy(values[1].data, 'unique_id')));

下划线没有_.merge()您可以通过没有它的每个属性都有循环。



Answer 3:

我不认为角或强调有这样的功能。 我会做类似下面的伪代码:

tempObject = {}
for object in objectArray
  if tempObject[object.unique_id] isnt undefined
    tempObject[object.unique_id] = object
  else angular.extend(tempObject[object.unique_id], object) // or the other way around depending on your preference
resultingArray = []
for object, key of tempObject
  resultingArray.push(object)

你将不得不运行for object in objectArray两个返回数组但应该工作,可能是更有效的比大多数合并算法,顶多它会遍历每个返回阵列的两倍。



文章来源: Merge objects with different values using Angularjs or Underscore js