首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代数组

迭代数组
EN

Stack Overflow用户
提问于 2017-04-18 04:55:49
回答 2查看 98关注 0票数 1

有没有一种方法可以迭代多个数组并从每个数组返回不同的值?

例如:

代码语言:javascript
复制
{
"gameQuestion": [
    "English League Championship: What will be the match result?",
    "2017 Boston Marathon: Which COUNTRY will the MEN'S WINNER represent?",
    "MLB: Who will WIN this matchup?",
    "English League Championship (Huddersfield Town @ Derby County): Will Derby SCORE in the 2nd Half?",
    "English Premier League: What will be the match result?",
    "MLB: Who will WIN this matchup?",
    "NBA Eastern Conference Playoffs - 1st Rd (Cavaliers lead 1-0): Who will WIN this matchup?",
    "NBA (IND@CLE): Which PLAYER will SCORE a HIGHER PERCENTAGE of their TEAM'S TOTAL POINTS in the 1st Half?",
    "NHL Eastern Conference Playoffs - 1st Rd (Series tied 1-1): Who will WIN this matchup?",
    "NHL Eastern Conference Playoffs - 1st Rd (Series tied 1-1): Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "NBA (IND@CLE): Will a 3-POINTER be MADE in the FIRST 2 MINUTES of the 3rd Quarter?",
    "NBA Western Conference Playoffs - 1st Rd (Spurs lead 1-0): What will be the GAME RESULT?",
    "NHL Western Conference Playoffs - 1st Rd (Predators lead 2-0): Who will WIN this matchup?",
    "NHL Western Conference Playoffs - 1st Rd (Ducks lead 2-0): Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?"
],
"propVal": [
    "m57338o58525",
    "m57338o58526",
    "m57336o58521",
    "m57336o58522",
    "m57329o4111",
    "m57329o12",
    "m57316o793",
    "m57316o726",
    "m57319o58515",
    "m57319o58516",
    "m57322o423",
    "m57322o461",
    "m57323o517",
    "m57323o515",
    "m57327o206",
    "m57327o15",
    "m57330o14",
    "m57330o35",
    "m57331o21",
    "m57331o148",
    "m57298o27453",
    "m57298o112",
    "m57320o58517",
    "m57320o58518",
    "m57318o58513",
    "m57318o58514",
    "m57325o481",
    "m57325o479",
    "m57326o463",
    "m57326o5964",
    "m57333o19384",
    "m57333o78",
    "m57334o3",
    "m57334o5"
],
"info": [
    "Opponents",
    " Aston Villa: Win or Draw",
    "@ Fulham: Win",
    "Kenya",
    " Any Other Country",
    " Tampa Bay Rays (6-7) Snell",
    " @ Boston Red Sox (7-5) Wright",
    " Yes: Derby Scores 1+ Goals in 2nd Half",
    " No: No Derby Goal in 2nd Half",
    " Arsenal: Win",
    " @ Middlesbrough: Win or Draw",
    " Chicago White Sox (6-5) Holland",
    " @ New York Yankees (8-4) Montgomery",
    " Indiana Pacers (42-40)",
    " @ Cleveland Cavaliers (51-31)",
    " Paul George (IND)",
    " LeBron James (CLE) or Tie",
    " Ottawa Senators (44-28-10)",
    " @ Boston Bruins (44-31-7)",
    " Washington Capitals (55-19-8)",
    " @ Toronto Maple Leafs (40-27-15)",
    " Pittsburgh Pirates (6-6) Nova",
    " @ St. Louis Cardinals (3-9) Lynn",
    " Milwaukee Brewers (7-6) Anderson",
    " @ Chicago Cubs (6-6) Lackey",
    " Cleveland Indians (5-7) Salazar",
    " @ Minnesota Twins (7-5) Gibson",
    " Los Angeles Angels (6-7) Chavez",
    " @ Houston Astros (8-4) Morton",
    " Yes: 3PM in First 2 Min of 3rd Qtr",
    " No: No 3PM in First 2 Min of 3rd Qtr",
    " Grizzlies: Win or Single Digit Loss",
    " @ Spurs: Win By Double Digits",
    " Chicago Blackhawks (50-23-9)",
    " @ Nashville Predators (41-29-12)",
    " Anaheim Ducks (46-23-13)",
    " @ Calgary Flames (45-33-4)",
    " Miami Marlins (7-5) Koehler",
    " @ Seattle Mariners (5-8) Miranda",
    " Arizona Diamondbacks (8-5) Ray",
    " @ Los Angeles Dodgers (7-6) McCarthy"
]

}

我想在同一时间重复三次,但是返回

["English League Championship: What will be the match result?", Aston Villa: Win or Draw","@ Fulham: Win",m57338o58525",m57338o58526]然后是[2017 Boston Marathon: Which COUNTRY will the MEN'S WINNER represent?","Kenya"," Any Other Country","m57336o58521", "m57336o58522"],在b上,我需要跳过第一个元素。

代码语言:javascript
复制
var json = require('./output.json');

var a = json.gameQuestion;
var b = json.info;
var c = json.propVal;
var res= [];
for(var i = 0; i < a.length; i++){
res.push([a[i],b[i*2+1],b[i*2+2],c[i*2],c[i*2+1]]);
}
console.log(res[0]);
console.log(res[1]);
console.log(res[2]);
console.log(res[3]);
console.log(res[4]);
console.log(res[5]);

我得到了第一次迭代,但每次添加另一个for循环时,返回j的次数与第一个for循环的长度相同。

更新:谢谢!这个问题解决了!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-18 05:17:59

如果我没搞错你,看看这个

代码语言:javascript
复制
var a = [1,2,3]
var b = [69,4,5,6,7,8,9]
var c = [10,11,12,13,14,15]
var res= [];
for(var i = 0; i<a.length; i++){
res.push([a[i],b[i*2+1],b[i*2+2],c[i*2],c[i*2+1]]);
}
console.log(res[0]);
console.log(res[1]);
console.log(res[2]);

票数 1
EN

Stack Overflow用户

发布于 2017-04-18 05:28:46

你可以试试这个:

代码语言:javascript
复制
var a = [1,2,3]
var b = [69,4,5,6,7,8,9]
var c = [10,11,12,13,14,15]
//Since for each value in array a you need a different value in array b in a single iteration we will use different pointers for each array but value will be dependent on i     
for(var i=0;i<a.length;i++){
    var final = [];
    j = 2*i+1; //j is the pointer that iterates over array 'b'
    k = 2*i; //k iterates over array 'c'

    final.push(a[i]);
    final.push(b[j]);
    final.push(b[j+1]);
    final.push(c[k]);
    final.push(c[k+1]);
    console.log(final);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43463844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档