Using map to iterate through two arrays

后端 未结 4 1878
日久生厌
日久生厌 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:59

    Generally, what you're looking for is a zip function, such as the one that lodash provides. Much like a real zipper, it combines two things of the same length into one:

    const zipped = _.zip(sentences, icons);
    
    return (
      
    {zipped.map(([sentence, icon], index) => ( {text}; ))}
    );

    Note, this is doing more iterations than are technically needed. If performance is an issue, you may want a solution that's a bit smart (not really in scope for your question though).

提交回复
热议问题