-2

I have two separate arrays of data I need to merge into a single array where the values of keys match. I am creating the two arrays via the functions below as the data comes from two separate areas, but should be related.

Example of desired merge by 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", }]

Here is my current code that creates the two arrays:

    $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());
        });
        }
Kode
  • 3,073
  • 18
  • 74
  • 140

1 Answers1

2

Here you go.

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});
        }
    }
}
Alex M
  • 494
  • 5
  • 14