Home > Net >  How to convert a js object into an array?
How to convert a js object into an array?

Time:08-08

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.

CodePudding user response:

const data = [quantity];

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

CodePudding user response:

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"}]}
  • Related