Javascript ES6 - map multiple arrays

后端 未结 3 1686
甜味超标
甜味超标 2021-01-07 22:23

Is there a feature in JavaScript 6 that allows to map over multiple arrays ?

Something like a zipper :

 var myFn = function (a, b) { console.log(a, b         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-07 22:54

    You could also use reduce to get the desired outcome:

    var arr1 = ['a', 'b', 'c'];
    var arr2 = [1, 2, 3];
    
    arr1.reduce((acc, current, index) => {
       console.log(current, arr2[index])
       return [...acc, current, arr2[index]]
    }, [])
    
    // a 1
    // b 2
    // c 3
    // returns ["a", 1, "b", 2, "c", 3]
    

提交回复
热议问题