最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lodash flatMap to flatten object - Stack Overflow

programmeradmin1浏览0评论

I have the following object:

const arr = [{
 "@id": "6005752",
 employeeId: {
  id: "22826"
},
allocationIntervals: {
 jobTaskTimeAllocationInterval: {
  "@id": "34430743",
   startTime: "2017-03-15T01:50:00.000Z",
   endTime: "2017-03-15T02:50:00.000Z"
 },
   "@id": "34430756",
   startTime: "2017-04-16T02:50:00.000Z",
   endTime: "2017-04-16T03:50:00.000Z"
},
taskId: {
 id: "16465169"
}
}];

I am trying to pull out all of the start and end times from allocationIntervals.jobTaskTimeAllocationInterval to create something like the following:

const arr = [{
  employeeId: "22826",
  taskId: "16465169"
  startTime: "2017-03-15T01:50:00.000Z",
  endTime: "2017-03-15T02:50:00.000Z"
  },
  {
  employeeId: "22826",
  taskId: "16465169",
  startTime: "2017-04-16T02:50:00.000Z",
  endTime: "2017-04-16T03:50:00.000Z"   
}];

I'm looking at doing this with Lodash flatMap, with something like the following function:

const result = _.flatMap(arr, item => {
  return _.map(item.allocationIntervals, allocation => _.defaults({ start: item.jobTaskTimeAllocationInterval.startTime }, allocation));
});

Does anyone know of a way to solve the above?

I have the following object:

const arr = [{
 "@id": "6005752",
 employeeId: {
  id: "22826"
},
allocationIntervals: {
 jobTaskTimeAllocationInterval: {
  "@id": "34430743",
   startTime: "2017-03-15T01:50:00.000Z",
   endTime: "2017-03-15T02:50:00.000Z"
 },
   "@id": "34430756",
   startTime: "2017-04-16T02:50:00.000Z",
   endTime: "2017-04-16T03:50:00.000Z"
},
taskId: {
 id: "16465169"
}
}];

I am trying to pull out all of the start and end times from allocationIntervals.jobTaskTimeAllocationInterval to create something like the following:

const arr = [{
  employeeId: "22826",
  taskId: "16465169"
  startTime: "2017-03-15T01:50:00.000Z",
  endTime: "2017-03-15T02:50:00.000Z"
  },
  {
  employeeId: "22826",
  taskId: "16465169",
  startTime: "2017-04-16T02:50:00.000Z",
  endTime: "2017-04-16T03:50:00.000Z"   
}];

I'm looking at doing this with Lodash flatMap, with something like the following function:

const result = _.flatMap(arr, item => {
  return _.map(item.allocationIntervals, allocation => _.defaults({ start: item.jobTaskTimeAllocationInterval.startTime }, allocation));
});

Does anyone know of a way to solve the above?

Share Improve this question edited Nov 7, 2017 at 20:37 Jim Dover asked Nov 7, 2017 at 18:39 Jim DoverJim Dover 6233 gold badges18 silver badges32 bronze badges 2
  • Can you correct the data structure that you have there to put brackets around the arrays? It's a bit unclear whether allocationIntervals or jobTaskTimeAllocationInterval is the array. – Gruff Bunny Commented Nov 7, 2017 at 20:27
  • Sorry! - I have updated with square brackets wrapping the original. This is how the array is returning from an API. – Jim Dover Commented Nov 7, 2017 at 20:37
Add a ment  | 

1 Answer 1

Reset to default 4

It looks like for every item in arr you need 2 elements in the output array; one for allocationIntervals and one for allocationIntervals.jobTaskTimeAllocationInterval. Each one has the same employeeId and taskId from the item itself.

Create a function that will return an output item given an item and an allocation:

const createAllocation = (item, allocation) => ({
    employeeId: item.employeeId.id, 
    taskId: item.taskId.id, 
    startTime: allocation.startTime, 
    endTime: allocation.endTime
});

Call this function twice for each item passing the allocationIntervals for the first call and allocationIntervals.jobTaskTimeAllocationInterval for the second call:

const result = _.flatMap(arr, item => [
    createAllocation(item, item.allocationIntervals), 
    createAllocation(item, item.allocationIntervals.jobTaskTimeAllocationInterval)
]);
发布评论

评论列表(0)

  1. 暂无评论