I am using promises in javascript and for the resolve variable I am passing a multidimensional array. But for some reason my array changes in the then function. Here is my code,
Id_name_disp = [[], []];
var list = $("#Approversdisp").val().split(", ");
var promise = new Promise(function(resolve, reject){
for(var i = 0; i < list.length-1; i++){
$.ajax({
"url" :"private",
"type" : "GET",
"data" : {"name": list[i].trim()},
"contentType" : "application/json",
"success" : function(data) {
Id_name_disp[0] = Id_name_disp[0].concat(data[0]);
Id_name_disp[1] = Id_name_disp[1].concat(data[1]);
resolve(Id_name_disp);
},
"error" : function(jqXHR, textStatus, errorThrown)
{
reject(textStatus+": "+jqXHR.responseText);
}
});
}
});
promise.then(function(fromresolve){
console.log(fromresolve);
console.log(fromresolve[0]);
console.log(fromresolve);
console.log(fromresolve[0].length);
for(var k = 0; k < list.length-1; k++){
var ind = -1;
for(var x = 0; x < fromresolve[0].length; x++){
console.log("x: "+x);
console.log(fromresolve[0][x]+" "+list[k]);
ind = fromresolve[0][x].indexOf(list[k]);
console.log(ind);
if(ind > -1){
console.log("found: "+ind);
break;
}
}
sapid+=" "+fromresolve[0][ind].substring(fromresolve[0][ind].indexOf("("), fromresolve[0][ind].indexOf(")")+1)+",";
console.log(sapid);
}
}).catch(function(fromreject){
alert(fromreject);
});
fromresolve is multidimensional array where the 0 position is an array of 6 elements and 1st position is an array of 6 elements. but when I do fromresolve[0] I get an array with one element. Why is that happening. The expected result is fromresolve[0] will be an array of 6 elements.