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

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

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.

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.

Share Improve this question edited Jul 21, 2014 at 11:12 Andy 63.6k13 gold badges71 silver badges98 bronze badges asked Jul 21, 2014 at 11:08 KathySKathyS 131 gold badge1 silver badge4 bronze badges 6
  • what is [i] in the hash of sampleItems? Is it value that you assume you will have in for loop? :P – RAJ Commented Jul 21, 2014 at 11:15
  • It would be useful if you provided the code for for loop you have tried to put into sampleItems. – Andrey Shchekin Commented Jul 21, 2014 at 11:16
  • i will be a value! I just want a basic for loop var streams = 7 for(i=0; i<streams; i++) – KathyS Commented Jul 21, 2014 at 11:18
  • Have a look stackoverflow./questions/24863630/… – RAJ Commented Jul 21, 2014 at 11:29
  • @KathyS As a good learner and contributor, if you found any answer useful, you should upvote for that – RAJ Commented Aug 4, 2014 at 17:05
 |  Show 1 more ment

2 Answers 2

Reset to default 4

As per conversation in question ments, 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

Well you are looping over an array containing one object as entry. what you probably want to do is to discard you object structure pletely and just use an simple array like:

var sampleItems = [ sampleGroups[0], "Item Title: 1", ..... ];

you could also make it a an actual object without using arrays but it seems to me that you want to use the List. If no List ist necessary just put the whole object genereated by generateSampleData into you object or append it to an existing object.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744218443a4563664.html

相关推荐

  • How to fill an array with a for loop in Javascript? - Stack Overflow

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

    8天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信