Tengo dos arreglos de videos y storeProducts. La matriz de videos puede tener product_id y storeProducts debe tener product_id. Necesito fusionar estos en función de produt_id, de lo contrario, solo empujar elementos de video en la matriz.

     let videos = [
      {
        "id": 1,
        "product_id":"training_video_test_1",
        "video_title": "Video title 1",
        "video_short_description": "desc 1"
      },
      {
        "id": 2,
        "product_id":"training_video_test_2",
        "video_title": "Video title 2",
        "video_short_description": "desc 2"
      },
      {
        "id": 3,
        "product_id":"training_video_test_3",
        "video_title": "Video title 3",
        "video_short_description": "desc 3"
      }
     
  ];
  
  let storeProducts = [
      {
          "product_id":"training_video_test_1",
          "prduct_title":"training_video_test_1",
          "price":100
      },
      {
        "product_id":"training_video_test_2",
        "prduct_title":"training_video_test_2",
        "price":100
    }
  ];

Necesito fusionar estos dos cuando storeProducts.product_id === videos.product_id de lo contrario, ignore los productos de la tienda. Los artículos solo envían artículos de video.

Ejemplo de salida:

[
      {
        "id": 1,
        "product_id":"training_video_test_1",
        "video_title": "Video title 1",
        "video_short_description": "desc 1",
        "prduct_title":"training_video_test_1",
        "price":100
      },
      {
        "id": 2,
        "product_id":"training_video_test_2",
        "video_title": "Video title 2",
        "video_short_description": "desc 2",
        "prduct_title":"training_video_test_2",
        "price":100
      },
      {
        "id": 3,
        "product_id":"training_video_test_3",
        "video_title": "Video title 3",
        "video_short_description": "desc 3"
      }
  ]

Lo he intentado así:

let resultArr = [];
videos.forEach((v)=>{
  storeProducts.forEach((p)=>{
  if(p.product_id == v.product_id){
      resultArr.push(Object.assign({},v,p))
    
    }
  })
})

Pero no funciona como esperaba, por favor ayúdame.

0
Hari 27 ago. 2020 a las 15:52

3 respuestas

La mejor respuesta

En su lugar, puede utilizar mapa

let videos = [
    { id: 1, product_id: "training_video_test_1", video_title: "Video title 1", video_short_description: "desc 1" },
    { id: 2, product_id: "training_video_test_2", video_title: "Video title 2", video_short_description: "desc 2" },
    { id: 3, product_id: "training_video_test_3", video_title: "Video title 3", video_short_description: "desc 3" },
];

let storeProducts = [
    { product_id: "training_video_test_1", prduct_title: "training_video_test_1", price: 100 },
    { product_id: "training_video_test_2", prduct_title: "training_video_test_2", price: 100 },
];

const result = videos.map(v => ({ ...v, ...storeProducts.find(sp => sp.product_id === v.product_id) }));

console.log(result);
1
Nikita Madeev 27 ago. 2020 a las 12:59

Utilice el operador de propagación.

let resultArr = [];
videos.forEach((v)=>{
    storeProducts.forEach((p)=>{
        if(p.product_id == v.product_id){
            resultArr.push({...v, ...p})

        }
    })
})
0
nikals 27 ago. 2020 a las 12:59

Le recomiendo que evite un find y un operador de propagación para cada elemento dentro del controlador (rendimiento muy bajo), puede transformar la matriz storeProducts en un objeto de valor clave que proporciona una forma más rápida de acceder a los objetos .

let videos = [    {      "id": 1,      "product_id":"training_video_test_1",      "video_title": "Video title 1",      "video_short_description": "desc 1"    },    {      "id": 2,      "product_id":"training_video_test_2",      "video_title": "Video title 2",      "video_short_description": "desc 2"    },    {      "id": 3,      "product_id":"training_video_test_3",      "video_title": "Video title 3",      "video_short_description": "desc 3"    }],
    storeProducts = [    {      "product_id":"training_video_test_1",      "prduct_title":"training_video_test_1",      "price":100    },    {      "product_id":"training_video_test_2",      "prduct_title":"training_video_test_2",      "price":100  }],
    mapped = storeProducts.reduce((a, c) => (a[c.product_id] = c, a), {}),
    result = videos.map(o => Object.assign(o, mapped[o.product_id]));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
0
Ele 27 ago. 2020 a las 13:15