我有两个独立的数据数组,我需要合并到一个数组中,其中键的值匹配。我正在通过以下函数创建两个数组,因为数据来自两个独立的区域,但应该是相关的。
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());
});
}
答案 0 :(得分:2)
你走了。
var ar1 = [{'test':'1', 'test2':'alex'},{'test':'2', 'test2':'ana'}];
var ar2 = [{'test3':'1', 'test4':'lol'},{'test3':'2', 'test4':'rofl'}];
var ar3 = [];
for (var i1 = 0; i1 < ar1.length; i1++) {
for (var i2 = 0; i2 < ar2.length; i2++) {
if (ar1[i1].test == ar2[i2].test3) {
ar3.push({'test':ar1[i1].test, 'test2': ar1[i1].test2, 'test4': ar2[i2].test4});
}
}
}