如何合并“123”和“Trilby hat”中的对象
{
"123": {
"Price": 68.9,
"Qty": 2,
"ProductID": 858383
},
"Trilby hat": {
"Price": 21.67,
"Qty": 1,
"ProductID": 858236
}
}
到
{
"Items" : \[
{
"Price": 68.9,
"Qty": 2,
"ProductID": 858383
},
{
"Price": 21.67,
"Qty": 1,
"ProductID": 858236
}
\]
}
只需获取条目值,并将它们分配给
Items
。
const actual = { Items: Object.values(raw) };
const raw = {
"123": {
"Price": 68.9,
"Qty": 2,
"ProductID": 858383
},
"Trilby hat": {
"Price": 21.67,
"Qty": 1,
"ProductID": 858236
}
};
const expected = {
"Items": [{
"Price": 68.9,
"Qty": 2,
"ProductID": 858383
}, {
"Price": 21.67,
"Qty": 1,
"ProductID": 858236
}]
};
const actual = { Items: Object.values(raw) };
console.log(JSON.stringify(actual) === JSON.stringify(expected));