x = torch.max(x,torch.tensor([0.]))
我想要这样
[
{
customerId: 20
customerName: "customer 1"
orderItems: [
{
productId: '23'
productName: 'ice cream'
price: '200'
},
....
]
},
customerId: 21
customerName: "customer 2"
orderItems: [
{
productId: '47'
productName: 'bottle'
price: '60'
},
{
productId: '48'
productName: 'shake'
price: '544'
},
....
]
},
]
有人可以在这方面帮助我吗?我已经尝试过地图运算符,但无法遍历内部数组。 谢谢
答案 0 :(得分:1)
您可以采用Array#flatMap
方法并获取非规范化数据。
var data = [{ customerId: 20, customerName: "customer 1", orderItems: [{ productId: '23', productName: 'ice cream', price: '200' }] }, { customerId: 21, customerName: "customer 2", orderItems: [{ productId: '47', productName: 'bottle', price: '60' }, { productId: '48', productName: 'shake', price: '544' }] }],
denormalized = data.flatMap(({ orderItems, ...customer }) =>
orderItems.map(order => ({ ...customer, ...order })));
console.log(denormalized);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
您可以使用.map()
和.flat()
方法来获得所需的输出:
const data = [{
customerId: 20,
customerName: "customer 1",
orderItems: [{
productId: '23',
productName: 'ice cream',
price: '200'
}]
}, {
customerId: 21,
customerName: "customer 2",
orderItems: [{
productId: '47',
productName: 'bottle',
price: '60'
}, {
productId: '48',
productName: 'shake',
price: '544'
}]
}];
const result = data.map(
({orderItems, ...rest}) => orderItems.map(o => Object.assign({}, rest, o))
).flat();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }