JavaScript Using map to iterate through two arrays

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 => (<Component icon={item.image} text={item.text} />) )

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 (<Component icon={image} text={text} />);
});
Answer:1

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 <ListGroupItem key={index}>{text} <img src={icons[index]} /i> </ListGroupItem>;
        })
return (
     <div>
       <ListGroup>
        {sentenceList}
      </ListGrouup>
   </div>
);

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

Answer:2

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)

Answer:3

I want to add dynamic directive inside another directive tempalte. As you see I want to add another directive inside a directive template How do add those dynamic directive there Please help return {...

I want to add dynamic directive inside another directive tempalte. As you see I want to add another directive inside a directive template How do add those dynamic directive there Please help return {...

I have read several tutorials about closures. Some tutorials have some information about execution context. I am confused that execution context and closure seem to the same thing.Does anyone can ...

I have read several tutorials about closures. Some tutorials have some information about execution context. I am confused that execution context and closure seem to the same thing.Does anyone can ...

  1. closure execution context

I have a ScatterChart where I need to draw a diagonal line (of the function y=x) that serves as a visual aid. Is it possible ? if so, how can I do it?

I have a ScatterChart where I need to draw a diagonal line (of the function y=x) that serves as a visual aid. Is it possible ? if so, how can I do it?

  1. draw line google maps
  2. draw line google docs
  3. draw line google earth pro
  4. draw line google maps api
  5. draw line google maps javascript
  6. draw line google maps between two points
  7. draw line google earth
  8. draw line google maps android
  9. draw line google maps coordinates
  10. draw line google sketchup
  11. line draw google chart
  12. draw latitude line google maps
  13. draw straight line google maps
  14. draw line distance google maps
  15. draw dashed line google maps android
  16. draw latitude line google earth
  17. draw a line google
  18. draw straight line google earth
  19. draw a line google maps ios
  20. google maps draw polyline

I'm really confused about importing a VueJs component. I'm using Vuejs 2.2.4, and I need to import a Vuejs component. Here is my app.js: Vue.component('Test', require('./Test.vue')); const app = ...

I'm really confused about importing a VueJs component. I'm using Vuejs 2.2.4, and I need to import a Vuejs component. Here is my app.js: Vue.component('Test', require('./Test.vue')); const app = ...

  1. dynamic import component vuejs
  2. import multiple components vuejs
  3. import css in component vuejs
  4. vuejs import component global
  5. vuejs import component without webpack
  6. vuejs import component webpack
  7. import component in another component vuejs
  8. vuejs import component unexpected identifier