-2
У меня есть два отдельных массива данных, которые мне нужно объединить в один массив, в котором совпадают значения ключей. Я создаю два массива с помощью следующих ниже функций, поскольку данные поступают из двух отдельных областей, но должны быть связаны.Объединить два массива по значениям ключа с помощью JavaScript
Пример желаемого сливаться 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? – vjdhama
Да, это то, что я ищу. – Kode
Каков ваш код? – axelduch