title: How to keep Array order when doing a foreach loop in Angularjs
body: <p>The order of my array always changes when i do a foreach loop. How can I keep the order of the array.</p>
  
  <pre><code>var array1 = [{id:1, name:'foo'},{id:2, name:'bar'},{id:3, name:'lol'}]
  </code></pre>
  
  <p>After i do a foreach and output it to a new array, the order sometimes changes</p>
  
  <pre><code>var array2 = [];
  
  angular.forEach(array1, function(post) {
    //for brevity i'll just keep it simple
     var sample = {id:post.id, name:post.name};
     array2.push(sample);
  });
  </code></pre>
  
  <hr>
  
  <pre><code>//OUTPUT
  var array2 = [{id:3, name:'lol'},{id:1, name:'foo'},{id:2, name:'bar'}]
  </code></pre>
  
  <p>My question is how can i iterate without changing the order.</p>