Unnest array of objects contained in object with Javascript.

huangapple go评论67阅读模式
英文:

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 = [
  {
    &quot;shipmentNumber&quot;:12345678,
    &quot;items&quot;:[
      {
        &quot;productId&quot;:80000000000001,
        &quot;price&quot;:100,
        &quot;quantity&quot;:1
      },
      {
        &quot;productId&quot;:80000000000002,
        &quot;price&quot;:80,
        &quot;quantity&quot;:1
      }
    ]
  },
  {
    &quot;shipmentNumber&quot;:89675645,
    &quot;items&quot;:[
      {
        &quot;productId&quot;:80000000000003,
        &quot;price&quot;:100,
        &quot;quantity&quot;:1
      }
    ]
  }
];

const result = shipments.flatMap(shipment =&gt;
  shipment.items.map(item =&gt; ({
    shipmentNumber: shipment.shipmentNumber,
    item: item
  }))
);

console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 19:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定