Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
Display data from another array inside map function

I have two arrays:

const Array1 = [ 
{id:1, sold: 69,  productName: 'Epic' image: 'someimage.svg' } ,
{id:2, sold: 121,  productName: 'Legend' image: 'someimage.svg' } ,
{id:3, sold: 368,  productName: 'Top' image: 'someimage.svg' }
]

const Array2 = [ 
{ productBrand: 'Chiquita' , productName: 'Epic' , productCategory:'Banana' } ,
{ productBrand: 'Famoozo' , productName: 'Legend' , productCategory: 'Mango' } ,
{ productBrand: 'PinkLady' , productName: 'Top' , productCategory: 'Apple' }
]

And i want to display data in my JSX from both of them. I gave it a try with Array.map in map , but that is not the solution that i am after.

JSX

...
return (
    <> 
     {
       Array1.map((dataFromFirstArray) => 
        Array2.map((dataFromSecondArray) => (
          <div>
            <img src={dataFromFirstArray.img} />
            <p> {dataFromFirstArray.sold} sales </p>
             <p> {dataFromSecondArray.productBrand} </p>
             <p> {dataFromSecondArray.productName} </p>
             <p> {dataFromSecondArray.productCategory} </p>
          </div>
       )
     }
    </>
)

UPDATE

How can i combine the array's into one if productName is the same in both of them. At the end of the day i want to use all the data but coming from one array.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It's better if you merge it in a single array and than map that

like this

const Array1 = [ 
{id:1, sold: 69,  productName: 'Epic', image: 'someimage.svg' } ,
{id:2, sold: 121,  productName: 'Legend', image: 'someimage.svg' } ,
{id:3, sold: 368,  productName: 'Top', image: 'someimage.svg' }
]

const Array2 = [ 
{ productBrand: 'Chiquita' , productName: 'Epic' , productCategory:'Banana' } ,
{ productBrand: 'Famoozo' , productName: 'Legend' , productCategory: 'Mango' } ,
{ productBrand: 'PinkLady' , productName: 'Top' , productCategory: 'Apple' }
]

const products = Array1.map(({productName, ...rest}) => {
  return {
    ...rest,
    ...Array2.find(p => p.productName === productName) || {}
  }
})

console.log(products)

//and then

/*products.map((p) => 
        
          <div>
            <img src={p.img} />
            <p> {p.sold} sales </p>
             <p> {p.productBrand} </p>
             <p> {p.productName} </p>
             <p> {p.productCategory} </p>
          </div>
       )
     
    </>
    */

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is my answer.

{Array1.map((dataFromFirstArray, i) =>
      Array2.map((dataFromSecondArray, j) => {
        if (j === i) {
          return (
            <div>
              <p> {dataFromFirstArray.id}</p>
              <img src={dataFromFirstArray.image} />
              <p> {dataFromFirstArray.sold} sales </p>
              <p> {dataFromSecondArray.productBrand} </p>
              <p> {dataFromSecondArray.productName} </p>
              <p> {dataFromSecondArray.productCategory} </p>
            </div>
          );
        }
      })
    )}

It's working fine for me please try once at your end.

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar

0

So i guess the answer is pretty simple Because we have the data like from the both array's like this :

const Array1 = [ 
{id:1, sold: 69,  productName: 'Epic' image: 'someimage.svg' } ,
{id:2, sold: 121,  productName: 'Legend' image: 'someimage.svg' } ,
{id:3, sold: 368,  productName: 'Top' image: 'someimage.svg' }
]

const Array2 = [ 
{ productBrand: 'Chiquita' , productName: 'Epic' , productCategory:'Banana' } ,
{ productBrand: 'Famoozo' , productName: 'Legend' , productCategory: 'Mango' } ,
{ productBrand: 'PinkLady' , productName: 'Top' , productCategory: 'Apple' }
]

Instead of concatenating it we map over and we search uppon keys that match. So this is my solution :

let Array3 = totalSales.map((item, i) => Object.assign({}, item, soldStatsData[i]));

And the final result is like this :

console.log(Array3)
--------------------
[
 {  
   id:1, 
   sold: 69, 
   productName: 'Epic', 
   productBrand: 'Chiquita', 
   productCategory:'Banana' 
   image: 'someimage.svg' 
} ,
{  
   id:2, 
   sold: 121,  
   productName: 'Legend',
   productBrand: 'Famoozo',
   productCategory: 'Mango' 
   image: 'someimage.svg',
} ,
{
   id:3, 
   sold: 368,  
   productBrand: 'PinkLady',
   productName: 'Top',
   productCategory: 'Apple',
   image: 'someimage.svg'
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda