Unnest array of objects contained in object with Javascript.

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

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

  1. [
  2. {
  3. "shipmentNumber":12345678,
  4. "items":[
  5. {
  6. "productId":80000000000001,
  7. "price":100,
  8. "quantity":1
  9. },
  10. {
  11. "productId":80000000000002,
  12. "price":80,
  13. "quantity":1
  14. }
  15. ]
  16. },
  17. {
  18. "shipmentNumber":89675645,
  19. "items":[
  20. {
  21. "productId":80000000000003,
  22. "price":100,
  23. "quantity":1
  24. }
  25. ]
  26. }
  27. ]

This is what I want to achieve:

N.B.: The main array containing shipments has 5000+ elements

  1. [
  2. {
  3. "shipmentNumber":12345678,
  4. "items": {
  5. "productId":80000000000001,
  6. "price":100,
  7. "quantity":1
  8. }
  9. },
  10. {
  11. "shipmentNumber":12345678,
  12. "items":
  13. {
  14. "productId":80000000000002,
  15. "price":80,
  16. "quantity":1
  17. }
  18. },
  19. {
  20. "shipmentNumber":89675645,
  21. "items":
  22. {
  23. "productId":80000000000003,
  24. "price":100,
  25. "quantity":1
  26. }
  27. }
  28. ]

答案1

得分: 2

使用组合的 flatMap/map 应该能实现您的目标:

  1. result = a.flatMap(
  2. s => s.items.map(
  3. item => ({shipmentNumber: s.shipmentNumber, item})
  4. ))
英文:

A combination flatMap/map should do the trick:

  1. result = a.flatMap(
  2. s => s.items.map(
  3. item => ({shipmentNumber: s.shipmentNumber, item})
  4. ))

答案2

得分: 1

你可以使用 .map() 方法将每个嵌套数组中所需的数据映射出来,并使用 .flatMap() 方法将结果数组展平为单一数组。

  1. const shipments = [
  2. {
  3. "shipmentNumber": 12345678,
  4. "items": [
  5. {
  6. "productId": 80000000000001,
  7. "price": 100,
  8. "quantity": 1
  9. },
  10. {
  11. "productId": 80000000000002,
  12. "price": 80,
  13. "quantity": 1
  14. }
  15. ]
  16. },
  17. {
  18. "shipmentNumber": 89675645,
  19. "items": [
  20. {
  21. "productId": 80000000000003,
  22. "price": 100,
  23. "quantity": 1
  24. }
  25. ]
  26. }
  27. ];
  28. const result = shipments.flatMap(shipment =>
  29. shipment.items.map(item => ({
  30. shipmentNumber: shipment.shipmentNumber,
  31. item: item
  32. }))
  33. );
  34. 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 -->

  1. const shipments = [
  2. {
  3. &quot;shipmentNumber&quot;:12345678,
  4. &quot;items&quot;:[
  5. {
  6. &quot;productId&quot;:80000000000001,
  7. &quot;price&quot;:100,
  8. &quot;quantity&quot;:1
  9. },
  10. {
  11. &quot;productId&quot;:80000000000002,
  12. &quot;price&quot;:80,
  13. &quot;quantity&quot;:1
  14. }
  15. ]
  16. },
  17. {
  18. &quot;shipmentNumber&quot;:89675645,
  19. &quot;items&quot;:[
  20. {
  21. &quot;productId&quot;:80000000000003,
  22. &quot;price&quot;:100,
  23. &quot;quantity&quot;:1
  24. }
  25. ]
  26. }
  27. ];
  28. const result = shipments.flatMap(shipment =&gt;
  29. shipment.items.map(item =&gt; ({
  30. shipmentNumber: shipment.shipmentNumber,
  31. item: item
  32. }))
  33. );
  34. 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:

确定