迭代数组

时间:2017-04-18 04:55:50

标签: javascript arrays

有没有办法迭代多个数组并从每个数组返回不同的值?

例如:

["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"]然后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]); 和b我需要跳过第一个元素。

let ContactNumber: NSString = (phoneNumber.value).value(forKey:"digits") as! NSString
print(ContactNumber)

let localizedLabel = CNLabeledValue<NSString>.localizedString(forLabel: phoneNumber.label!)
print(localizedLabel)

我已经完成了第一次迭代,但是有时我会添加另一个for循环,它最终会返回j与第一个for循环的长度相同的次数。

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

2 个答案:

答案 0 :(得分:1)

如果我没错你,请查看

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 :(得分:0)

你可以试试这个:

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);
}