-4

What's wrong with my code shown below. It doesn't work on IE 8 or lower. How can I change it with plain for loop instead of forEach?

var mainItems = [];
[100305, 1003403, 1003511, 1003360, 1002328].forEach(function(id) {
//mainItems.forEach(function(id) {
    mainItems.push(items.filter(function(elem) {
        return elem.id == id;
    })[0])
})
CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
user1942626
  • 805
  • 1
  • 9
  • 15
  • Come on. Try to search at least a little bit http://stackoverflow.com/questions/52080/how-do-i-build-a-loop-in-javascript – awbergs Mar 20 '13 at 16:44
  • Have you looked at any of the basic documentation as far as doing `for` loops in JavaScript? This is a *very* basic question that you're asking. – Vivin Paliath Mar 20 '13 at 16:45
  • Hey men, you guys sooooo smart???? I asked that because I had no idea. – user1942626 Mar 22 '13 at 12:04

2 Answers2

1

you can use simply as

 var mainItems = [];

var items = [100305, 1003403, 1003511, 1003360, 1002328];


for(var i=0;i<items.length;i++)   
  mainItems.push(items[i]);
PSR
  • 39,804
  • 41
  • 111
  • 151
0

Array.forEach is part of the ES5 specification, which is not supported in older browsers. If you plan on using ES5 code, you should look at including a shim, which adds support for non-supporting browsers.

As for how to write a for loop, the documentation can help you there.

Matt
  • 74,352
  • 26
  • 153
  • 180