I currently have a forEach loop like this.
var videoUrls ={};
ytplayer.config.args.url_encoded_fmt_stream_map.split(',')
.forEach(function(item) {
var obj = { };
item.split('&')
.forEach(function(param) {
param = param.split('=');
obj[param[0]] = decodeURIComponent(param[1]);
});
videoUrls[obj.quality] = obj;});
Since IE is not supporting forEach loop, I tried to convert this to for loop.
var videoUrls ={};
var typea= ytplayer.config.args.url_encoded_fmt_stream_map.split(',');
for (var item=0; item<typea.length; item++){
var obj= {};
var typeb= typea[item].split('&');
for (var param=0; param<typeb.length; param++){
typeb[param]= typeb[param].split('=');
obj[typeb[0]] = decodeURIComponent(typeb[1]);
}
videoUrls[obj.quality]= obj;
}
But when I run the script the results were different. What did i do wrong?
Thanks in advance.
it should be:
typeb[param]= typeb[param].split('=');
obj[typeb[param][0]] = decodeURIComponent(typeb[param][1]);
Because the other loop is:
param = param.split('=');
obj[param[0]] = decodeURIComponent(param[1]);
Not:
obj[item.split("&")[0]] = decodeURIComponent(item.split("&")[1])
If it's still not clear, here is a simpler explanation:
typeb === item.split("&");
typeb[param] === param;
The problem is here:
for (var param=0; param<typeb.length; param++) {
typeb[param]= typeb[param].split('=');
obj[typeb[0]] = decodeURIComponent(typeb[1]);
}
You're overwriting an element of the array you're iterating over ( typeb[param]
) and then using a hardcoded index into the same array ( typeb[1]
)
Should be more like:
for (var param=0; param<typeb.length; param++) {
var arr = typeb[param].split('=');
obj[arr[0]] = decodeURIComponent(arr[1]);
}
And you cannot use library like jQuery? It is for this reason jQuery is built. If you cannot then i think this is what you need to do obj[typeb[param][0]] = decodeURIComponent(typeb[param][1]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.