Using map to iterate through two arrays

后端 未结 4 1876
日久生厌
日久生厌 2021-01-07 06:07

Currently in React, I am using array.map(function(text,index){}) to iterate through an array. But, how am I going to iterate through two arrays simultaneously u

4条回答
  •  离开以前
    2021-01-07 07:01

    You can't do this with built-in Array.prototype methods, but you can use something like this:

    function map2(arr1, arr2, func) {
        return arr1.map(
            (el, i) => { return func(el, arr2[i]); }
        );
    }
    

    (Of course, arr1 and arr2 are expected to have the same length)

提交回复
热议问题