英文:
Unnest array of objects contained in object with Javascript
问题
这是我的数据结构。一个包含物品数组的运输数组。我需要有一个包含每个运输物品的数组,并且我需要使用JavaScript来完成这个任务。
这是我想要实现的:
注意: 包含运输的主数组有5000多个元素。
英文:
This is the structure of my data. An array of shipments containing array of items
I need to have an array with entries for each shipment-item and I need to do this with javascript
[
	{
		"shipmentNumber":12345678,
		"items":[
			{
				"productId":80000000000001,
				"price":100,
				"quantity":1
			},
			{
				"productId":80000000000002,
				"price":80,
				"quantity":1
			}
		]
	},
	{
		"shipmentNumber":89675645,
		"items":[
			{
				"productId":80000000000003,
				"price":100,
				"quantity":1
			}
		]
	}
]
This is what I want to achieve:
N.B.: The main array containing shipments has 5000+ elements
[
	{
		"shipmentNumber":12345678,
		"items": {
				"productId":80000000000001,
				"price":100,
				"quantity":1
			}
	},
	{
		"shipmentNumber":12345678,
		"items":
			{
				"productId":80000000000002,
				"price":80,
				"quantity":1
			}
	},
	{
		"shipmentNumber":89675645,
		"items":
			{
				"productId":80000000000003,
				"price":100,
				"quantity":1
			}
	}
]
答案1
得分: 2
使用组合的 flatMap/map 应该能实现您的目标:
result = a.flatMap(
    s => s.items.map(
        item => ({shipmentNumber: s.shipmentNumber, item})
    ))
英文:
A combination flatMap/map should do the trick:
result = a.flatMap(
    s => s.items.map(
        item => ({shipmentNumber: s.shipmentNumber, item})
    ))
答案2
得分: 1
你可以使用 .map() 方法将每个嵌套数组中所需的数据映射出来,并使用 .flatMap() 方法将结果数组展平为单一数组。
const shipments = [
  {
    "shipmentNumber": 12345678,
    "items": [
      {
        "productId": 80000000000001,
        "price": 100,
        "quantity": 1
      },
      {
        "productId": 80000000000002,
        "price": 80,
        "quantity": 1
      }
    ]
  },
  {
    "shipmentNumber": 89675645,
    "items": [
      {
        "productId": 80000000000003,
        "price": 100,
        "quantity": 1
      }
    ]
  }
];
const result = shipments.flatMap(shipment =>
  shipment.items.map(item => ({
    shipmentNumber: shipment.shipmentNumber,
    item: item
  }))
);
console.log(result);
这段代码将嵌套的数据映射为一个单一数组,并打印出结果。
英文:
You can use .map() to map the needed data in each nested array,and .flatMap() method to flatten the result arrays into the single array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const shipments = [
  {
    "shipmentNumber":12345678,
    "items":[
      {
        "productId":80000000000001,
        "price":100,
        "quantity":1
      },
      {
        "productId":80000000000002,
        "price":80,
        "quantity":1
      }
    ]
  },
  {
    "shipmentNumber":89675645,
    "items":[
      {
        "productId":80000000000003,
        "price":100,
        "quantity":1
      }
    ]
  }
];
const result = shipments.flatMap(shipment =>
  shipment.items.map(item => ({
    shipmentNumber: shipment.shipmentNumber,
    item: item
  }))
);
console.log(result);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论