【发布时间】:2015-05-19 19:28:21
【问题描述】:
我有两个单独的数据数组,我需要将它们合并到一个键值匹配的数组中。我通过下面的函数创建两个数组,因为数据来自两个不同的区域,但应该是相关的。
所需的 ID 合并示例:
array1 = [{"name": "bob", "id":"1"}, {"name": "sue", "id":"2"}]
array2 = [{"location": "vegas", "id":"1"}, {"location": "texas", "id":"2"}]
desiredarray = [{"name": "bob", "id":"1", "location": "vegas"}, {"name": "sue", "id":"2", "location": "texas", }]
这是我当前创建两个数组的代码:
$scope.termsArray = [];
$scope.labelsArray = [];
execOperation();
function execOperation() {
//Current Context
var context = SP.ClientContext.get_current();
//Current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Term Stores
var termStores = taxSession.get_termStores();
//Name of the Term Store from which to get the Terms.
var termStore = termStores.getByName("Taxonomy1111");
//GUID of Term Set from which to get the Terms.
var termSet = termStore.getTermSet("1111");
var terms = termSet.getAllTerms();
context.load(terms);
context.executeQueryAsync(function () {
var termEnumerator = terms.getEnumerator();
//var termList = "Terms: \n";
while (termEnumerator.moveNext()) {
var currentTerm = termEnumerator.get_current();
var guid = currentTerm.get_id();
var guidString = guid.toString();
$scope.termsArray.push({
termName: currentTerm.get_name(),
termGUID: guidString,
termSynonyms: 'Coming Soon'
});
getLabels(guid);
//termList += currentTerm.get_id() + currentTerm.get_name() + "\n";
}
//alert(termList);
}, function (sender, args) {
console.log(args.get_message());
});
}
function getLabels(termguid) {
var clientContext = SP.ClientContext.get_current();
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext );
var termStores = taxSession.get_termStores();
var termStore = termStores.getByName("Taxonomy1111");
var termSet = termStore.getTermSet("1111");//change the term set guid here
var term = termSet.getTerm(termguid);
var labelColl = term.getAllLabels(1033);
clientContext.load(labelColl);
clientContext.executeQueryAsync(function () {
var labelEnumerator = labelColl.getEnumerator();
while (labelEnumerator.moveNext()) {
var label = labelEnumerator.get_current();
var value = label.get_value();
// array push function where value is passed?
$scope.labelsArray.push({
termLabel: value,
termGUID: termguid
});;
}
}, function (sender, args) {
console.log(args.get_message());
});
}
【问题讨论】:
-
要根据id属性合并吗?
-
是的,这就是我要找的。span>
-
到目前为止你的代码是什么?
-
我还没有编写任何合并代码,但包括我当前的更新代码。
标签: javascript arrays