1

My data:

{product_quantity: {quantity: 13, code: "AAA", warehouse: "1000",}}

product_quantity is an field from a mongodb json data.

I need to convert it into this:

{"product_quantity": [{"quantity": 13, "code": "AAA", "warehouse": "1000"}]

How can I transform an object into array with its keys and values?

Thanks.

8
  • 1
    Does this answer your question? How to convert JSON object to JavaScript array? Commented Aug 8, 2022 at 13:40
  • 1
    [quantity]? Hard to tell where the difficulty is... Commented Aug 8, 2022 at 13:45
  • No, I need the same variable to be converted. Not creating an array and inserting data into it Commented Aug 8, 2022 at 13:50
  • 1
    quantity = [quantity]; It's till hard to tell what you are trying to do and what you're having difficulty with. Commented Aug 8, 2022 at 13:51
  • 1
    Read How to Ask and create a proper minimal reproducible example of the problem Commented Aug 8, 2022 at 13:57

2 Answers 2

1

json you want is not valid , you can use this

let productQuantity={product_quantity:{quantity:13, code:"AAA", warehouse:"1000"}};

productQuantity.product_quantity=[productQuantity.product_quantity];

console.log(JSON.stringify(productQuantity));

result

{"product_quantity":[{"quantity":13,"code":"AAA","warehouse":"1000"}]}
Sign up to request clarification or add additional context in comments.

Comments

1

const data = [quantity];

output; [{quantity: 13, code: "AAA", warehouse: "1000",}];

2 Comments

I've edited the question, I think it is now clearer to understand. Thanks for the help
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.