This code works, but I feel like it's really hacky and could be done a lot better. Can anyone give some advice on how this could be neater?$scope.arrays = function() {        $scope.committees = [];        $scope.employees = [];        $http({method: 'GET', url: 'data.php'}).success(function(data) {            angular.forEach(data, function(value, key) {                $scope.committees.push(value);            })            $http({method: 'GET', url: 'data2.php'}).success(function(data){                angular.forEach(data, function(value, key) {                    $scope.employees.push(value);                })                $scope.arr = new Array();                for (var i = 0, committee; committee = $scope.committees[i]; i++) {                    for (var j = 0, employee; employee = $scope.employees[j]; j++) {                            if (committee.code === employee.code){                                $scope.arr.push({                                    committee : committee.name,                                    color: committee.color,                                    members: committee.members,                                    femaleAnnual : employee.femaleAnnual,                                    maleAnnual : employee.maleAnnual,                                    totalAnnual : employee.totalAnnual,                                })                            }                    }                }            })        })    }    $scope.arrays();Edit: Sorry I should have been clearer! What I'm trying to do here is merge 2 arrays if they have the same value for 'code' - and then push that array to the scope.
Matching and pushing array
javascript;array;angular.js
You could take advantage of the fact that push accepts more than one argument so you only call push once. Build the arguments array and use apply.Array.prototype.push.apply($scope.committees, Object.keys(data).map(function(key){  return data[key];}));Or if you're just putting values instead of actually appending to the existing array:$scope.committees = Object.keys(data).map(function(key){  return data[key];});Now for the AJAX part, you don't need to have a sequential request, that's that last thing you'd want to do in AJAX (besides making them synchronous). You'll need something like Promise.all or jQuery's $.when, fire them at the same time and resolve when all is done.var comitteePromise = $http({method: 'GET', url: 'data.php'});var employeePromise = $http({method: 'GET', url: 'data2.php'});Promise.all([comitteePromise, employeePromise]).then(function(values){  var committees = values[0];  var employees = values[1];  ...});The further I look into your code, what you're just doing is merging the data from both arrays. You can build a lookup for one array, and iterate the other. That way, two loops and not to the power of two loops.var comitteePromise = $http({method: 'GET', url: 'data.php'});var employeePromise = $http({method: 'GET', url: 'data2.php'});Promise.all([comitteePromise, employeePromise]).then(function(values){  var committees = values[0];  var employees = values[1];  // Use the committee code (assuming it's unique) as a key for lookup  var committeesByCode = committees.reduce(function(carry, committee){    carry[committee.code] = committee;    return carry;  }, {});  // For each employee, lookup the committee and add in the data  $scope.arr = employees.map(function(employee){    var committee = committeesByCode[employee.code];    return {      committee: committee.name,      color: committee.color,      members: committee.members,      femaleAnnual: employee.femaleAnnual,      maleAnnual: employee.maleAnnual,      totalAnnual: employee.totalAnnual    };  });});