1

I want to create new Object attributes in this loop.
Like

This is what I want to achieve

response.product.product.0
response.product.product.0.quantity
response.product.product.0.id
response.product.product.0.description

response.product.product.1
response.product.product.1.quantity
response.product.product.1.id
response.product.product.1.description

and so on for every iteration of the loop i need to create new attributes. But the iteration are random it can be between 1 and 4 iterations ...

for (let i = 0; i < Math.floor(Math.random() * 4); i++) {
      const salesOrderItem = await createItem(OBJECT_ID, csfrToken, cookies,
      validProducts.pop())
      const PRODUDUCT_DESCRIPTION = salesOrderItem.data.d.results.Description
      const PRODUCT_ID = salesOrderItem.data.d.results.ProductID
      const PRODUCT_QUANTITY = Math.trunc(salesOrderItem.data.d.results.Quantity)
      response['product' + i] = PRODUCT_ID
      response['productDescription' + i] = PRODUDUCT_DESCRIPTION
      response['productQuantity' + i] = PRODUCT_QUANTITY
    }

This is what I got so far but its not realy what I want

0

2 Answers 2

0

Try this:

response = {product:{product:{} }}
    for (let i = 0; i < Math.floor(Math.random() * 4); i++) {
          const salesOrderItem = await createItem(OBJECT_ID, csfrToken, cookies,
          validProducts.pop())
          const PRODUDUCT_DESCRIPTION = salesOrderItem.data.d.results.Description
          const PRODUCT_ID = salesOrderItem.data.d.results.ProductID
          const PRODUCT_QUANTITY = Math.trunc(salesOrderItem.data.d.results.Quantity)
          response.product.product[i] = {};
          response.product.product[i]['id'] = PRODUCT_ID
          response.product.product[i]['quantity'] = PRODUCT_QUANTITY
          response.product.product[i]['description'] = PRODUDUCT_DESCRIPTION
        }
Sign up to request clarification or add additional context in comments.

Comments

0

This is what you get with your code:

response.product0 = PRODUCT_ID
response.productDescription0 = PRODUDUCT_DESCRIPTION
response.productQuantity0 = PRODUCT_QUANTITY

... or something like this, but with a number diferent of '0'.

To get the desired format, you need to change your code to:

response = {product: { product: {} }}

for (let i = 0; i < Math.floor(Math.random() * 4); i++) {
      const salesOrderItem = await createItem(OBJECT_ID, csfrToken, cookies,
      validProducts.pop())
      const PRODUDUCT_DESCRIPTION = salesOrderItem.data.d.results.Description
      const PRODUCT_ID = salesOrderItem.data.d.results.ProductID
      const PRODUCT_QUANTITY = Math.trunc(salesOrderItem.data.d.results.Quantity)
      response.product.product[i] = {}
      response.product.product[i].id = PRODUCT_ID
      response.product.product[i].description = PRODUDUCT_DESCRIPTION
      response.product.product[i].quantity = PRODUCT_QUANTITY
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.