Search code examples
javascriptloopbackjsarray.prototype.map

how can i work with a lot data .push() for use it outside of .map()?


In this moment i have a problem. How can i push a lot of data for work with it outside of .map().

I need push a lot of data in const todo = [] and see data outside of .map() codigoP.map

i tried with .promise but i don't know how implement when one work in a push of lot data like this:

const todo = [];
let dateNow = moment();
let diferencia = 0;

codigoP.map(async (item)=> {
  const listaPrecio = await models.listas_precios.findOne({
    where: {
      producto_id: item.id,
      cliente_id: empresa.id
    }
  });
  let precioIva = 0;
  if (listaPrecio) {
    precioIva = listaPrecio.iva;
  }
  cant += parseInt(detDevolucion.cantidadProducto);
  precioTotal = detDevolucion.precio_producto * parseInt(detDevolucion.cantidadProducto);
  const totalIva =  precioTotal + precioIva;
  total += totalIva;
  totalConIva += precioTotal;
  ivaTotal += precioIva;
  let fechaVencimiento = moment(item.fechaVencimiento).utc().toDate();
  diferencia = dateNow.diff(fechaVencimiento, 'days');
  todo.push({
    codigo: item.codigo,
    nombre: item.nombre,
    vidaUtil: diferencia,
    lote: detDevolucion.lote_id,
    cantidad: detDevolucion.cantidadProducto,
    precio: numeral(detDevolucion.precio_producto).format('$0,0.00'),
    precioTotal: numeral(precioTotal).format('$0,0.00'),
    iva: numeral(precioIva).format('$0,0.00'),
    totalIva: numeral(totalIva).format('$0,0.00'),
    observacion: detDevolucion.observacion,
    fechaVenc: dateFormat(item.fechaVencimiento, "yyyy-mm-dd")
  });
  console.log('todo: ', todo);// **this works, return the values**
  diferencia = 0;
});
  console.log('todo: ', todo); //**this return todo [], empty array, this is my problem.**

Solution

  • You can try something like this, as we've discussed above. Map returns a new array and return the object for each iteration.

    Edit: you can try using Promise.all to resolve the promises before returning.

    let dateNow = moment();
    let diferencia = 0;
    const todo = await Promise.all(codigoP.map(async (item)=> {
      const listaPrecio = models.listas_precios.findOne({
        where: {
          producto_id: item.id,
          cliente_id: empresa.id
        }
      });
      let precioIva = 0;
      if (listaPrecio) {
        precioIva = listaPrecio.iva;
      }
      cant += parseInt(detDevolucion.cantidadProducto);
      precioTotal = detDevolucion.precio_producto * parseInt(detDevolucion.cantidadProducto);
      const totalIva =  precioTotal + precioIva;
      total += totalIva;
      totalConIva += precioTotal;
      ivaTotal += precioIva;
      let fechaVencimiento = moment(item.fechaVencimiento).utc().toDate();
      diferencia = dateNow.diff(fechaVencimiento, 'days');
      return {
        codigo: item.codigo,
        nombre: item.nombre,
        vidaUtil: diferencia,
        lote: detDevolucion.lote_id,
        cantidad: detDevolucion.cantidadProducto,
        precio: numeral(detDevolucion.precio_producto).format('$0,0.00'),
        precioTotal: numeral(precioTotal).format('$0,0.00'),
        iva: numeral(precioIva).format('$0,0.00'),
        totalIva: numeral(totalIva).format('$0,0.00'),
        observacion: detDevolucion.observacion,
        fechaVenc: dateFormat(item.fechaVencimiento, "yyyy-mm-dd")
      };
      //console.log('todo: ', todo);// **this works, return the values**
      //diferencia = 0;
    }));
      console.log('todo: ', todo);