Using map to iterate through two arrays

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

    If at all possible, I would recommend storing the text alongside the images in an array of objects, eg:

    const objects = [{text: 'abc', image: '/img.png' }, /* others */];
    

    that way you can just iterate through the array and select both members at the same time, for example:

    objects.map(item => () )
    

    If this isn't possible then just map over one array and access the second array's members via the current index:

    sentences.map((text, index) => {
        const image = images[index];
        return ();
    });
    

提交回复
热议问题