0

I need to run function "testfun 2 times", for each function I will have a few of names lets say testfun(5, global_user) // output [1,2,4,4,5] and for testfun(7, global_user) // output [9,10,11] How I can put this 2 arrays in one array after I will run 2 functions?

testfun(5, global_user);
testfun(7, global_user);

function testfun(groupId, myUser) {
    var selectStr = "Title";
    var itemsUrl = "https://info.com(" + groupId + ")/users" + "?" + selectStr + "&" + orderbyStr;
    var executor = new SP.RequestExecutor;
    executor.executeAsync(
        {
         url: itemsUrl,
         method: "GET",
         headers: { "Accept": "application/json; odata=verbose" },
         success: loadTeamNames,
         error: errorHandler
        }
    );
}

var arr = [];
function loadTeamNames(data){
  var jsonObject = JSON.parse(data.body);
  var results = jsonObject.d.results;
  var hide_groups = false;
  $(results).each(function(){
    var name = $(this)[0].Name; 
  });   
}

Thanks

4 Answers 4

1

With JS

var mergedArray = outputOne.concat(outputTwo);

With JQuery

var mergedArray = $.merge( $.merge( [], outputOne), outputTwo);
Sign up to request clarification or add additional context in comments.

Comments

0

Since testFun() uses asynchronous methods you can't do anything immediately after running it twice without waiting for both instnces to complete. This is accomplished using promises

You could use $when() and return a promise from testFun(). Will need to move loadTeamNames into testFun to do it

$.when() won't complete until both of the promises are resolved

function testfun(groupId, myUser) {

    var defer = $.Deferred();

    var selectStr = "Title";
    var itemsUrl = "https://info.com(" + groupId + ")/users" + "?" + selectStr + "&" + orderbyStr;
    var executor = new SP.RequestExecutor;
    executor.executeAsync(
            {
                url : itemsUrl,
                method : "GET",
                headers : {"Accept" : "application/json; odata=verbose"},
                success : loadTeamNames,
                error : errorHandler
            }
    );
    function loadTeamNames(data) {
        var jsonObject = JSON.parse(data.body);
        var results = jsonObject.d.results;
        var hide_groups = false;
        $(results).each(function () {
            var name = $(this)[0].Name;
        });

        // resolve deferred and pass data to be used in `$.when()`
        defer.resolve(results);
    }

    return defer.promise;

}

To use

$.when(testfun(5, global_user),testfun(7, global_user)).done(function (results1, results2) {

    //do what you need to with arrays results1 & results2

});

Add defer.reject() in the errorHandler

5 Comments

Thanks but lets say can I do in this way: $.when(testfun(5, global_user),testfun(7, global_user)).done(function () { console.log(login_name); }); i just want an output of all numbers in one array
looks the same as mine ... isn't it?
you can also add data to the resolve() if you want to pass it to done(). Not really clear what you needed to do in question. So pass results ... defer.resolve(results) and both arrays will be in done(function(results1, results2){...
Ok I try now to put a console.log in the return: $.when(testfun(5, global_user),testfun(7, global_user)).done(function () { console.log(login_name); }); But the problem is that this variable login_name is not global may be or if I put it global after that i have undefind
I don't know enough of your code to help without seeing it.
0

Assuming that jsonObject.d.results is an array already, you can just do:

arr.concat(results)

This will concatenate your array so far with the new result. Have that code inside of loadTeamNames and each run of testfun will concatenate the result to your current array. not really sure what you're using all those variables inside of loadTeamNames for however.

Comments

0
function getTeamNames(groupId, myUser) {
    var defer = $.Deferred();
    var selectStr = "$select=Title,LoginName";
    var orderbyStr = "$orderby=Title";
    var itemsUrl = "https://sites.sp.kp.org/pub/qosstgcat/_api/web/SiteGroups/getbyid(" + groupId + ")/users" + "?" + selectStr + "&" + orderbyStr;
    var executor = new SP.RequestExecutor(carootUrl);
    executor.executeAsync(
            {
                url : itemsUrl,
                method : "GET",
                headers : {"Accept" : "application/json; odata=verbose"},
                success : loadTeamNames,
                error : errorHandler
            }
    );
    function loadTeamNames(data) {
        var jsonObject = JSON.parse(data.body);
        var results = jsonObject.d.results;
        var hide_groups = false;
        $(results).each(function(){
         var login_name = $(this)[0].LoginName;
        });
        defer.resolve(results);
    }
    return defer.promise;
}

result

$.when(getTeamNames(4, global_user),getTeamNames(185, global_user)).done(function () {
        console.log(results);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.