0

New using Axios. I need help with the body formatting of my Axios post in React Js

My code:

  body: JSON.stringify(
    {
      quote_timestamp: timestamp,
      number_of_products: state.length,
    } &&
    state.map((product) => ({
      quote_timestamp: timestamp,
      number_of_products: state.length,
      products: {
        cat_Number: product.cat,
        product_Name: product.Name,
      },
    }))
  )

At the moment it is printing the data, as it maps each product like this:

body [
  {
    quote_timestamp: '7/24/2022, 11:25:27 PM',
    number_of_products: 3,
    products: {
      cat: 'AB1',
      Name: 'Alpha'
    }
  },
  {
    quote_timestamp: '7/24/2022, 11:25:27 PM',
    number_of_products: 3,
    products: {
      cat: 'AB2',
      Name: 'Beta'
    }
  },
  {
    quote_timestamp: '7/24/2022, 11:25:27 PM',
    number_of_products: 3,
    products: {
      cat: 'AB3',
      Name: 'Gamma'
    }
  }
]

However, I would like the format to be such that the timestamp and number of products are printed once and then maps through all selected products. Like this below:

    body[ {
        quote_timestamp: '7/24/2022, 11:25:27 PM',
        number_of_products: 3,
        products: { 

    { cat: 'AB1', Name:'Alpha' }, 
    
    { cat: 'AB2' Name:'Beta' }.
    
    { cat: 'AB3' Name:'Gamma' }
 }
}
5
  • so, not really a question about reactjs, json, api or axios - it's really just "how do I get the javascript object I want from the javascript object I have?" ... first issue I see is {some object} && {some other object} will result in JSON.stringify processing {some other object} ONLY - however, the output you WANT is not possible ... should products: be an Array rather than an Object? Commented Jul 24, 2022 at 22:58
  • How can I get JSON.stringify to process both ? Any suggestion? Commented Jul 24, 2022 at 23:01
  • Both? no you mean you want a single object to stringify ... also, the output you WANT is not possible - and one more thing, the code you've shown wouldn't produce the output you've shown, since the "products" object should have cat_Number, product_Name and Price properties, not cat and Name ... so, not sure what your real code is, but it's not this Commented Jul 24, 2022 at 23:03
  • I have amended it to just show the structure. But it is meant to be cat number and product name. I have edited the code. Commented Jul 24, 2022 at 23:07
  • no, you haven't yet asked for output that could possibly be produced by JSON.stringify .... what you want is not valid ... [ with no ] ... products: { { .... }, { .... } } is not valid either - it's difficult to help you if what you want is an object that is impossible Commented Jul 24, 2022 at 23:08

1 Answer 1

1

To get close to what you want, you would simply do this

JSON.stringify({
    quote_timestamp: timestamp,
    number_of_products: state.length,
    total_price: total,
    products: state.map((product) => ({
        cat_Number: product.Cat_Number,
        product_Name: product.Product_Name,
        Price: product.mockPrice,
    })),
})

Hope that helps

edit: oops, typo

Sign up to request clarification or add additional context in comments.

3 Comments

This is very close to the outcome I was looking for thank you!
However, it does not print the object details. Here is the output I get: body { quote_timestamp: '7/25/2022, 1:28:06 AM', number_of_products: 2, total_price: 2001, products: [ [ [Object], [Object] ] ] }
sorry, ... typo on my part - try now @mstarluk

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.