我怎样才能让John,Liza,Peter在for循环之外?
var siblings = ["John", "Liza", "Peter"];
for (var i=0; i < siblings.length; i++) {
names = siblings[i];
console.log(names) // correctly outputs John, Liza, Peter
}
console.log(names) // only gives Peter
更新:我试图在单元测试用例中重现问题。但这就是整个事情。
所以我发了一个请求,得到了一个数组
[ { kind: 'youtube#video',
etag: '"sZ5p5Mo8dPpfIzLYQBF8QIQJym0/XDKLx9FZygpwjYlzaTU-HAH31tw"',
id: 'b4Bj7Zb-YD4',
snippet:
{ publishedAt: '2016-10-28T07:00:01.000Z',
channelId: 'UCaHNFIob5Ixv74f5on3lvIw',
title: 'Calvin Harris - My Way (Official Video)',
description: 'Calvin Harris - My Way (Official Video)\nDownload My Way: http://smarturl.it/CHMyWay?IQid=yt\nListen to My Way: http://smarturl.it/StreamCH?IQid=yt\n\n---------\n\nFollow Calvin Harris:\nFacebook: https://www.facebook.com/calvinharris/ \nTwitter: https://twitter.com/CalvinHarris \nInstagram: https://www.instagram.com/calvinharris/ \nWebsite: http://calvinharris.com/ \n\n---------\n\nCalvin Harris - My Way (Lyrics)\n\nWhy wait \nTo say\nAt least I did it my way \nLie awake \nTwo faced \nBut in my heart I understand \nI made \nMy move \nAnd it was all about you \nNow I feel \nSo far removed \n\nYou were the one thing in my way \n\nMy way\nAway away away',
thumbnails: [Object],
channelTitle: 'CalvinHarrisVEVO',
tags: [Object],
categoryId: '10',
liveBroadcastContent: 'none',
localized: [Object] },
statistics:
{ viewCount: '8313145',
likeCount: '271119',
dislikeCount: '7364',
favoriteCount: '0' } },
{ kind: 'youtube#video',
etag: '"sZ5p5Mo8dPpfIzLYQBF8QIQJym0/c2pD6DG-ieqcYFEcGjGB31NYRKk"',
id: '0zGcUoRlhmw',
snippet:
{ publishedAt: '2016-10-24T14:00:01.000Z',
channelId: 'UCRzzwLpLiUNIs6YOPe33eMg',
title: 'The Chainsmokers - Closer ft. Halsey',
description: 'Collage EP: \nPre-order on Amazon: http://smarturl.it/CollageAmz\nUrban Outfitters White Vinyl: http://smarturl.it/CollageVinyl\n\n"Closer" ft. Halsey\niTunes: http://smarturl.it/CloseriT\nSpotify: http://smarturl.it/CloserStream\nApple Music: http://smarturl.it/CloserAM\nAmazon: http://smarturl.it/CloserAmz\n\nDirected by: Dano Cerny\n\nFollow The Chainsmokers:\nhttp://www.youtube.com/thechainsmokers\nhttp://www.twitter.com/thechainsmokers\nhttp://www.facebook.com/thechainsmokers\nhttp://www.instagram.com/thechainsmokers\nhttp://www.soundcloud.com/thechainsmokers\n\nFollow Halsey:\nTwitter: http://twitter.com/halsey\nFacebook: https://www.facebook.com/HalseyMusic\nInstagram: http://instagram.com/iamhalsey\nSpotify: http://smarturl.it/HalseySpotify\nNewsletter: http://www.iamhalsey.com/mailinglist\nPurchase Music: http://www.iamhalsey.com/badlandsbox',
thumbnails: [Object],
channelTitle: 'ChainsmokersVEVO',
tags: [Object],
categoryId: '10',
liveBroadcastContent: 'none',
localized: [Object] },
statistics:
{ viewCount: '21315027',
likeCount: '573642',
dislikeCount: '19913',
favoriteCount: '0',
commentCount: '40899' } }
]
所以我把它放在一个变量中,例如兄弟姐妹。然后我遍历整个数组,获取snippet.title,snippet.publishedAt。到目前为止一切都很好。但是我想将它推入一个新的数组,并使其可用于循环的OUTSIDE。
截至目前我的代码:
listVideos是我之前粘贴的一块。
listVideos.forEach(function list(item) {
data = new Array();
title = item.snippet.title;
return data.push(title);
});
console.log(data); // only gives first title. I want to get all titles in an array stored inside data. And make data available globally
在开头设置变量var数据对我来说不起作用,因为我在forEach循环之外还有其他函数。因此,它必须是一种灵活的方式,我可以移动到任何我想要的地方,例如某种功能范围。
感谢。
答案 0 :(得分:0)
它只说彼得,因为每次循环迭代你都会设置名称&#39;到那个特定的名字。如果你想获得所有的名字,那么&#39;名字&#39;必须是一个数组,你应该在循环中推送名称:
names.push(siblings[i])
答案 1 :(得分:0)
var names = new Array();
names.push(siblings[i]);
console.log(names);
答案 2 :(得分:0)
你不需要循环:
var elements = document.getElementById("newsletter-builder-area-center-frame-content").querySelectorAll("div.sim-row.ui-draggable");
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var hola = element.getAttribute("hola");
alert("Name: "+hola+"\r\n");
// var data_template['id_template']=hola;
var source_ = element.querySelectorAll('.sim-row-edit');
alert( source_.length);
for (var ab = 0; ab < source_.length; ab++) {
var source = source_[ab];
var type_source=source.getAttribute("data-type");
if (type_source === "image"){
var value=source.getAttribute("src");
alert(source);
alert(value);
}
else if (type_source === "image-link"){
// $(this).parent().parent();
var href_source=source.nextSibling.getAttribute('href');
var source_image=source.nextSibling.getAttribute('src');
alert(href_source);
alert(source_image);
}
else if(type_source === "link"){
var url=source.getAttribute("href");
var text_url=source.textContent;
var value=url+';'+text_url;
alert(value);
}
else if(type_source === "text"){
var value=source.innerHTML;
alert(value);
}
else if(type_source === "title"){
var value=source.textContent;
alert(value);
}
// var data_template[type_source_+ab]=value;
}
}