我如何处理大量数据 .push() 以便在 .map() 之外使用它?

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

此刻我遇到了一个问题。我如何在 .map() 之外推送大量数据以使用它。

我需要在 const todo = [] 中推送大量数据并查看 .map() 之外的数据 codigoP.map

我试过 .promise,但我不知道当像这样推送批次数据时如何实现:

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('[=10=],0.00'),
    precioTotal: numeral(precioTotal).format('[=10=],0.00'),
    iva: numeral(precioIva).format('[=10=],0.00'),
    totalIva: numeral(totalIva).format('[=10=],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.**

您可以像我们上面讨论的那样尝试类似的方法。映射 return 一个新数组和 return 每次迭代的对象。

编辑:您可以尝试在 returning 之前使用 Promise.all 解决承诺。

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('[=10=],0.00'),
    precioTotal: numeral(precioTotal).format('[=10=],0.00'),
    iva: numeral(precioIva).format('[=10=],0.00'),
    totalIva: numeral(totalIva).format('[=10=],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);