admin管理员组文章数量:1400503
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.
-
what is
[i]
in the hash ofsampleItems
? 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 intosampleItems
. – Andrey Shchekin Commented Jul 21, 2014 at 11:16 -
i
will be a value! I just want a basic for loopvar 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
2 Answers
Reset to default 4As 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.
本文标签: How to fill an array with a for loop in JavascriptStack Overflow
版权声明:本文标题:How to fill an array with a for loop in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744218443a2595748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论