Arrays 如何在req.body中的对象数组中迭代项?

Arrays 如何在req.body中的对象数组中迭代项?,arrays,node.js,object,express,request,Arrays,Node.js,Object,Express,Request,我有一个变量,我需要使用请求体填充它 var orderItem = { userPurchased: body.userPurchased, products:[{ product: body.products.product, size: body.products.size, quantity: body.products.quan

我有一个变量,我需要使用请求体填充它

        var orderItem = {
            userPurchased: body.userPurchased,
            products:[{
                product: body.products.product,
                size: body.products.size,
                quantity: body.products.quantity,
                subTotal: body.products.subTotal
            }],
            totalQuantity: body.totalQuantity,
            totalPrice: body.totalPrice,
            otherShipAd: body.otherShipAd,
            modeOfPayment: body.modeOfPayment
        };
这是我的请求体的样子

这里的问题是请求主体是一个对象数组,如果我按照上面显示的方式访问它,它将是未定义的。如果我像body.products.product[0]一样放置数组的编号,则所有记录都将相同


有人能告诉我如何迭代所有产品并将其放入变量中吗?

根据您的控制台响应

你应使用:

 var product;
    for(var i=0; i< body.products.length;i++)
    { product = [];
       product[product] =body.products[i].product[0];
    // set all properties
    }
    orderItem.products.push(product);

不确定我是否理解了您的困境,但从您的目标对象似乎与源对象具有完全相同的模式这一事实来看,为什么不将该值分配给您的var呢

var orderItem = request.body;
假设您仍然有理由需要从request.body创建该数组的副本?这仍然只是将request.body数组分配给目标变量的一行程序:

var orderItem = {
        userPurchased: body.userPurchased,
        products: body.products,
        totalQuantity: body.totalQuantity,
        totalPrice: body.totalPrice,
        otherShipAd: body.otherShipAd,
        modeOfPayment: body.modeOfPayment
    }; 

谢谢你的一行。也许我只是想得太多了。啊。可怜的我。