本文介绍了合并使用Angularjs或下划线的js不同的值对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想由UNIQUE_ID合并两个对象为在Angularjs控制器使用单一的多维对象。 (注意:我也有下划线的js加入)。
对象#1例如:
[
{UNIQUE_ID:001,称号:把绿色挑战 - 运动取决于部队和群众},
{UNIQUE_ID:002,称号:分子到生物:青蛙的生命周期}
]
对象#2的例子(有很多比1的对象更多的行..):
[
{ ab_id:76153F02-29F3-11D8-95EA-951BF95D9AEF,
UNIQUE_ID:001,
头衔:速度如何与能源,
状态:NY,
DOCUMENT_TITLE:核心课程
grade_ code:K-4,
grade_descr:小学,
STATE_ID:1.S2.3a,
state_text:使用适当的\\查询和操作技能\\收集数据},
{ ab_id:7980A762-29F3-11D8-BD14-861D7EA8D134,
UNIQUE_ID:001,
头衔:速度如何与能源,
状态:NY,
DOCUMENT_TITLE:核心课程
grade_ code:5-8
grade_descr:中级
STATE_ID:1.S3.2d,
state_text:规划和维护的解释和结论,因为它们与科学现象}
]
我的控制器:
abApp.controller(ABEE功能(abService,$ HTTP,$范围,$ Q _){
VAR abApp =这一点; $ scope.abData = $ http.get('/数据/ ab_activities.json',{
缓存:假的
});
$ scope.eeData = $ http.get('/活动/ EEDATA',{
缓存:假的
}); $ q.all([$ scope.eeData,$ scope.abData]),然后(功能(值){
VAR VAL =?这就是我想要的对象合并成一个大的多维对象..
});
下面是console.dir的输出(值);
0对象{数据= [28],状态= 200,配置= {...},更...}
1对象{数据= [743],状态= 200,配置= {...},更...}
这是所需的输出,我想尝试并获得:
[
{UNIQUE_ID:001,称号:把绿色挑战 - 运动取决于部队和群众,路线:[{ab_id:76153F02-29F3-11D8-95EA-951BF95D9AEF,UNIQUE_ID :001,称号:速度如何与能源,...},{ab_id:7980A762-29F3-11D8-BD14-861D7EA8D134,UNIQUE_ID:001,称号:速度如何与能源,...}]
]
解决方案
修改
您更新的问题后,我创造了这个
希望这是你的意思
要通过 UNIQUE_ID
VAR工会= {};$ q.all([$ scope.eeData,$ scope.abData]),然后(功能(值)
{
对于(VAR I = 0; I< values.length;我++)
{
VAR值=值[I]
如果(!工会[value.unique_id])
{
工会[value.unique_id] = {};
} angular.extend(工会[value.unique_id]值);
}
});//做财产以后与工会
...
I'm trying to merge two objects into a single multidimensional object for use in Angularjs controller by the 'unique_id'. (Note I also have Underscore Js added in).
Object #1 example:
[
{ "unique_id": "001", "title": "Putting Green Challenge - Motion Depends on Force and Mass" },
{ "unique_id": "002", "title": "Molecules to Organisms: Frog Life Cycle" }
]
Object #2 example (has MANY more rows than object 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"
}
]
My Controller:
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..
});
Here is the output of console.dir(values);
0 Object { data=[28], status=200, config={...}, more...}
1 Object { data=[743], status=200, config={...}, more...}
This is the desired output I'd like to try and get:
[
{ "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",...}]
]
解决方案
Edit
after you updated the question, i created this
hopes it's what you meant
To merge all objects by 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'
...
这篇关于合并使用Angularjs或下划线的js不同的值对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!