javascript - Create An Array Dynamically, Then Loop Through Another Array To Compare If Data Exist -
what trying achieve create array dynamically , use array compare array if data exist.
what happening right code compare arrays executing before array finish creating. comparing data empty array.
what technique can use make code execute code a
when code a
finished execute code b
?
var awayteamplayersplayedarray = []; vm.awayteamplayers = $firebasearray(awayteamplayersquery); // add data awayteamplayersplayedarray angular.foreach(vm.awayteamplayersplayed, function(value, key) { var awayteamplayerref = new firebase(firebase_url + 'players/' + key); var awayteamplayer = $firebaseobject(awayteamplayerref); awayteamplayer.$loaded().then(function() { awayteamplayersplayedarray.push(awayteamplayer.name); console.log(awayteamplayersplayedarray) }); }); // loop through vm.awayteamplayers see if exist in awayteamplayersplayedarray angular.foreach(vm.awayteamplayers, function(value, key) { if(awayteamplayersplayedarray.indexof(value.name) > -1) { console.log(value.name + ' yes'); } else { console.log(value.name + ' no'); } });
you should use loop after promises finished.
var allpromises = []; // add data awayteamplayersplayedarray angular.foreach(vm.awayteamplayersplayed, function(value, key) { var awayteamplayerref = new firebase(firebase_url + 'players/' + key); var awayteamplayer = $firebaseobject(awayteamplayerref); var promise = awayteamplayer.$loaded().then(function() { awayteamplayersplayedarray.push(awayteamplayer.name); console.log(awayteamplayersplayedarray); }); allpromises.push(promise); }); $q.all(allpromises).then(function() { // loop through vm.awayteamplayers see if exist in awayteamplayersplayedarray angular.foreach(vm.awayteamplayers, function(value, key) { if(awayteamplayersplayedarray.indexof(value.name) > -1) { console.log(value.name + ' yes'); } else { console.log(value.name + ' no'); } }); });
Comments
Post a Comment