reactjs 是否有一种方法可以使用对象的Key值对作为值来创建对象数组[duplicate]

zu0ti5jz  于 2023-02-03  发布在  React
关注(0)|答案(1)|浏览(136)
    • 此问题在此处已有答案**:

Convert object to an array of objects?(5个答案)
get Array-Object-String thing from the given Object(4个答案)
How to convert object A to an array of objects with object A's properties and assign value and label to them [duplicate](2个答案)
6小时前关门了。
我有一个目标

const myObject = {rice : 10, wheat: 20, sugar: 3}

我想把它转换成一个对象数组:

myArray = [
{"product" : rice, "QTY" : 10},
{"product" : wheat, "QTY" : 20},
{"product" : sugar, "QTY" : 3}
]

帮帮忙。谢谢。

4uqofj5v

4uqofj5v1#

这就是Object.entriesdestructuringshorthand property names可以提供的服务:

const myObject = {rice : 10, wheat: 20, sugar: 3}

const result = Object.entries(myObject).map(([product, qty]) => ({ product, qty }));  
console.log(result);

相关问题