使用Angularjs或Underscore js合并具有不同值的对象

时间:2014-07-31 04:12:16

标签: angularjs underscore.js

我尝试将两个对象合并为一个多维对象,以便在< unique_id'中用于Angularjs控制器。 (注意我也添加了Underscore 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示例(比对象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(values);

的输出
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",...}]
]

3 个答案:

答案 0 :(得分:1)

修改

更新问题后,我创建了 plunker

希望它是你的意思

enter image description here


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'
...

答案 1 :(得分:1)

如果您可以切换为使用lodash代替underscore,可以这样实现:

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

下划线没有_.merge(),你必须在没有它的情况下遍历每个属性。

答案 2 :(得分:0)

我不认为有角度或下划线有这种功能。我会做类似下面的伪代码:

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,但这应该有效,并且可能比大多数合并算法更有效,因为它最多会遍历每个返回的数组两次。