Reputation: 747
Is there a way to iterate over multiple arrays and return different values from each one?
Ex:
{
"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"
]
}
and I want to iterate over all three at the same time, but return
["English League Championship: What will be the match result?", Aston Villa: Win or Draw","@ Fulham: Win",m57338o58525",m57338o58526]
then [2017 Boston Marathon: Which COUNTRY will the MEN'S WINNER represent?","Kenya"," Any Other Country","m57336o58521",
"m57336o58522"]
and on b I need to skip the first element.
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]);
I've got the first iteration, but ever time I add another for loop it just ends up returning j the same amount of times as the length of the first for loop.
Update: Thanks! This problem is solved!
Upvotes: 1
Views: 107
Reputation: 4523
You can try this:
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);
}
Upvotes: 0
Reputation: 15604
If i didn't get you wrong, check this
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]);
Upvotes: 1