如何避免使用嵌套映射从嵌套数组中获取所需的值?

时间:2018-11-28 00:53:41

标签: javascript arrays ecmascript-6 lodash

我正在做这样的事情来获取我需要的值:

   tableRowsItems = data.softy.map(row =>
      row.items.map(item => console.log('item', item)),
    );

有了这个,我在下图中得到的正是我所需要的

enter image description here

我的问题是,这是否是正确的ES6方法。 如果有帮助,我也使用lodash。

这是不带地图的json文件的外观:

[
 {
  "ticketId": 67973802,
  "account": null,
  "items": [
    {
      "id": 11705294,
      "billingItemId": 361643044,
      "cancellationRequestId": 17289674,
      "immediateCancellationFlag": true,
      "scheduledCancellationDate": null,
      "serviceReclaimStatusCode": "COMPLETE",
      "billingItem": {
        "description": "Storage as a Service",
        "recurringFee": 0,
        "id": 361643044
      }
    }
  ]
 },
 ...
]
... 

我需要的是您在items键上看到的内容

4 个答案:

答案 0 :(得分:2)

如果我正确理解了您的问题,则您正在尝试从items对象数组中提取所有嵌套的ticket-在这种情况下,您可以通过将输入票证映射到的数组来实现items数组,然后将结果展平以将item对象的数组压缩为平坦,如下所示:

var output = input
.map(ticket => ticket.items) // Map each ticket to array of ticket items
.flat(); // Reduce array of item arrays to combined array of items

var input = [
{
  "ticketId": 67973802,
  "account": null,
  "items": [
    {
      "id": 11705294,
      "billingItemId": 361643044,
      "cancellationRequestId": 17289674,
      "immediateCancellationFlag": true,
      "scheduledCancellationDate": null,
      "serviceReclaimStatusCode": "COMPLETE",
      "billingItem": {
        "description": "Storage as a Service",
        "recurringFee": 0,
        "id": 361643044
      }
    }
  ]
 },
 {
  "ticketId": 67973802,
  "account": null,
  "items": [
    {
      "id": 11705294,
      "billingItemId": 361643044,
      "cancellationRequestId": 17289674,
      "immediateCancellationFlag": true,
      "scheduledCancellationDate": null,
      "serviceReclaimStatusCode": "COMPLETE",
      "billingItem": {
        "description": "Storage as a Service",
        "recurringFee": 0,
        "id": 361643044
      }
    }
  ]
 }
 ];
 
 var output = input.map(ticket => ticket.items).flat();
 
 console.log(output);

答案 1 :(得分:2)

Lodash具有flatMapnative JS版本尚未在所有浏览器上支持),您可以将其用作:

var items = _.flatMap(tickets, t => t.items);

以单个一维数组的形式从所有票证中获取所有items

var tickets = [{
    "ticketId": 67973802,
    "account": null,
    "items": [{
      "id": 117052912,
      "billingItemId": 36164304123,
    }, {
      "id": 11705232,
      "billingItemId": 361643044,
    }]
  },
  {
    "ticketId": 67973802,
    "account": null,
    "items": [{
      "id": 117052945,
      "billingItemId": 361643046,
    }, {
      "id": 117052953,
      "billingItemId": 361643049,
    }]
  }
];

var items = _.flatMap(tickets, t => t.items);
console.log(items);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

答案 2 :(得分:2)

使用 ES6 ,您可以轻松地执行一项功能,即通过Array.reduceitems属性来简化嵌套数组:

var tickets = [{ "ticketId": 67973802, "account": null, "items": [{ "id": 117052912, "billingItemId": 36164304123, }, { "id": 11705232, "billingItemId": 361643044, }] }, { "ticketId": 67973802, "account": null, "items": [{ "id": 117052945, "billingItemId": 361643046, }, { "id": 117052953, "billingItemId": 361643049, }] } ];
 
const pullBy = (arr, prop) => arr.reduce((r,c) => [...r[prop], ...c[prop]])
 
console.log(pullBy(tickets, 'items'))

使用lodash的最佳选择是flatMap

var tickets = [{ "ticketId": 67973802, "account": null, "items": [{ "id": 117052912, "billingItemId": 36164304123, }, { "id": 11705232, "billingItemId": 361643044, }] }, { "ticketId": 67973802, "account": null, "items": [{ "id": 117052945, "billingItemId": 361643046, }, { "id": 117052953, "billingItemId": 361643049, }] } ];
 
const result = _.flatMap(tickets, 'items')
 
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

答案 3 :(得分:2)

以上答案将帮助您实现目标,但是当我们想从循环中返回某些内容时,请使用.map,否则,您应该使用forEach

我知道对给定的问题确实无关紧要,但仍然如此。想要将其放入线程中。