简体   繁体   中英

Push 2 arrays after json loop

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

With JS

var mergedArray = outputOne.concat(outputTwo);

With JQuery

var mergedArray = $.merge( $.merge( [], outputOne), outputTwo);

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.

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

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM