Search code examples
javascriptarraysfor-loopwindows-8.1winjs

How to fill an array with a for loop in Javascript?


I want to create a for loop that will generate a new element for sampleItems based on a fixed number set in a for loop.

var list = new WinJS.Binding.List();

var groupedItems = list.createGrouped(
  function groupKeySelector(item) { return item.group.key; },
  function groupDataSelector(item) { return item.group; }
);

generateSampleData().forEach(function (item) {
  list.push(item);
});

function generateSampleData() { 

  var sampleGroups = [
    { key: "group1", title: "Event1", backgroundImage: "/images/event1.jpg"}
  ];

  var sampleItems = [
    { group: sampleGroups[0], title: "Item Title: 1", content: "http://192.168.201.41/Stream" + [i] + ".mp4", backgroundImage: "/images/image1.jpg" }
  ];

  return sampleItems;
}

I tried to place a for loop within sampleItems but i'm not allowed place the loop there.


Solution

  • As per conversation in question comments, here is the basic array population code for js:

    var streams = 7;
    var sampleItems = [];
    
    for(i = 0; i < streams; i++) {
       sampleItems.push({'a': 'b', 'c': 'd'})
    }
    

    Replace {'a': 'b', 'c': 'd'} with desired key-value pairs