Using map to iterate through two arrays

后端 未结 4 1916
日久生厌
日久生厌 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 06:48

    Are the both arrays of same length? You can do something like below if your intention is to combine both in some way.

    array.map(function(text,index){
       return text + ' ' + array2[index]
    })
    

    In your case:

    var sentenceList = sentences.map(function(text,index){
                return {text}  ;
            })
    return (
         
    {sentenceList}
    );

    Notice, How Icon src is being assigned. The idea is that access icons array with the same index to get a corresponding icon.

提交回复
热议问题