Home > OS >  Display data from another array inside map function
Display data from another array inside map function

Time:04-28

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.

CodePudding user response:

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>
       )
     
    </>
    */

CodePudding user response:

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

CodePudding user response:

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'
}
  • Related