数组filter,自定义需求和数据操作

const data = [{ employee: 70, month: 0, year: 2017, id: 3, createdAt: '2017-09-15T09:42:37.000Z', updatedAt: '2017-09-15T09:42:37.000Z', organization: 41, version: 1 }, { employee: 70, month: 4, year: 2017, id: 4, createdAt: '2017-09-15T09:59:28.000Z', updatedAt: '2017-09-15T09:59:28.000Z', organization: 41, version: 2 }, { employee: 70, month: 4, year: 2017, id: 5, createdAt: '2017-09-15T10:00:35.000Z', updatedAt: '2017-09-15T10:00:35.000Z', organization: 41, version: 3 }, { employee: 70, month: 4, year: 2017, id: 6, createdAt: '2017-09-15T10:01:18.000Z', updatedAt: '2017-09-15T10:01:18.000Z', organization: 41, version: 4 }, { employee: 70, month: 4, year: 2017, id: 7, createdAt: '2017-09-15T10:07:11.000Z', updatedAt: '2017-09-15T10:07:11.000Z', organization: 41, version: 5 }, { employee: 70, month: 4, year: 2017, id: 8, createdAt: '2017-09-15T10:40:11.000Z', updatedAt: '2017-09-15T10:40:11.000Z', organization: 41, version: 6 }, { employee: 70, month: 4, year: 2017, id: 9, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 7 }, { employee: 70, month: 7, year: 2017, id: 10, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 6 }, { employee: 70, month: 7, year: 2017, id: 11, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 7 }]; 

 const currentMonth = // 0, 11 

在这里,我需要做一个algorithm来获得数组的细节,

我想根据所需的月份号码从数组中获取详细信息。

  1. 如果月份编号与Array中存在的logging相匹配,则返回该月份,如果有多个月份的logging,则应通过查看版本的最高值来返回月份详情,假设在上面的数组中有7个月2版本6和7的logging,所以我想要的是最高版本7的对象。

    有7个月的logging2,所以我会得到

    { employee: 70, month: 7, year: 2017, id: 11, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 7 // <<==== highest version }

  2. 如果用户提供的月份号码不存在,那么应该查看最近的月份,并提供最高版本的对象,但是如果月份数量最less的对象应该是最高版本

    假设我想要5或6个月的logging,但在数组中没有该月的logging,所以我会寻找最低的月份是4,有4个月的多个logging所以,我会过滤我会想获取最高版本ID的对象

    这是

    { employee: 70, month: 4, year: 2017, id: 9, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 7 // <<======highest version }

  3. 根据此处的规则 ,如果提供的月份号不存在,且在前几个月没有logging,请从距离所提供的月份号最近的月份提供最高版本的对象。

我到目前为止所尝试的是这里。

这项工作看起来很简单,但我认为我使它更复杂..任何types的帮助是非常赞赏

 const getLastEmployeeMonthVersion = data.filter(function (emp, index) { if (emp.month < currentMonth) { return _.inRange(currentMonth, data[index].month, data[data.length - 1].month) ? emp : emp } }); employeeVersionForMonth = [...getLastEmployeeMonthVersion]; const getGroupByEmployee = employeeVersionForMonth.length && _.groupBy(employeeVersionForMonth, (v) => v.employee); const employeeKeys = Object.keys(getGroupByEmployee); let latestEmployeeVersion = []; employeeKeys.forEach(emp => { const maxVersionId = _.maxBy(getGroupByEmployee[emp], (value) => value.version); latestEmployeeVersion.push(maxVersionId); }) console.log(latestEmployeeVersion) 

你的意思是这样吗?

 const data = [{ employee: 70, month: 0, year: 2017, id: 3, createdAt: '2017-09-15T09:42:37.000Z', updatedAt: '2017-09-15T09:42:37.000Z', organization: 41, version: 1 }, { employee: 70, month: 4, year: 2017, id: 4, createdAt: '2017-09-15T09:59:28.000Z', updatedAt: '2017-09-15T09:59:28.000Z', organization: 41, version: 2 }, { employee: 70, month: 4, year: 2017, id: 5, createdAt: '2017-09-15T10:00:35.000Z', updatedAt: '2017-09-15T10:00:35.000Z', organization: 41, version: 3 }, { employee: 70, month: 4, year: 2017, id: 6, createdAt: '2017-09-15T10:01:18.000Z', updatedAt: '2017-09-15T10:01:18.000Z', organization: 41, version: 4 }, { employee: 70, month: 4, year: 2017, id: 7, createdAt: '2017-09-15T10:07:11.000Z', updatedAt: '2017-09-15T10:07:11.000Z', organization: 41, version: 5 }, { employee: 70, month: 4, year: 2017, id: 8, createdAt: '2017-09-15T10:40:11.000Z', updatedAt: '2017-09-15T10:40:11.000Z', organization: 41, version: 6 }, { employee: 70, month: 4, year: 2017, id: 9, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 7 }, { employee: 70, month: 7, year: 2017, id: 10, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 6 }, { employee: 70, month: 7, year: 2017, id: 11, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 7 }]; const currentMonth = 7; const monthLookup = data.reduce((prev, record) => { return record.month > prev && record.month <= currentMonth ? record.month : prev; }, 0); const record = data.filter((record) => record.month === monthLookup).reduce((prev, current) => { return prev.version > current.version ? prev : current; }); console.log(record); 

只需按照您的要求调用以下函数获取结果即可

 function findMonthRecords(month, recur) { var ret_records = []; for (i in data) { var record = data[i]; if (record["month"] == month) { ret_records.push(record) } } if (ret_records.length == 0 && recur) { for (var a = month - 1; a >= 0; a--) { ret_records = findMonthRecords(a, false) if (ret_records.length > 0) { return ret_records; } } for (var a = month + 1; a < 12; a++) { ret_records = findMonthRecords(a, false) console.log(a); console.log(ret_records); if (ret_records.length > 0) { return ret_records; } } } return ret_records; } console.log(findMonthRecords(2, true)); 

这是另一种实现该algorithm的方法。

1. filter数组与o.month <= currentMonth将列出具有月份的所有logging是小于当前月份。

2. orderBymonth ,然后version

3.从ordered列表中首先logging,这将成为您的logging。

 var result= _.head( _.orderBy( _.filter(data, function(o) { return o.month <= currentMonth; }), ['month', 'version'], ['desc', 'desc'])); 
 const data = [{ employee: 70, month: 0, year: 2017, id: 3, createdAt: '2017-09-15T09:42:37.000Z', updatedAt: '2017-09-15T09:42:37.000Z', organization: 41, version: 1 }, { employee: 70, month: 4, year: 2017, id: 4, createdAt: '2017-09-15T09:59:28.000Z', updatedAt: '2017-09-15T09:59:28.000Z', organization: 41, version: 2 }, { employee: 70, month: 4, year: 2017, id: 5, createdAt: '2017-09-15T10:00:35.000Z', updatedAt: '2017-09-15T10:00:35.000Z', organization: 41, version: 3 }, { employee: 70, month: 4, year: 2017, id: 6, createdAt: '2017-09-15T10:01:18.000Z', updatedAt: '2017-09-15T10:01:18.000Z', organization: 41, version: 4 }, { employee: 70, month: 4, year: 2017, id: 7, createdAt: '2017-09-15T10:07:11.000Z', updatedAt: '2017-09-15T10:07:11.000Z', organization: 41, version: 5 }, { employee: 70, month: 4, year: 2017, id: 8, createdAt: '2017-09-15T10:40:11.000Z', updatedAt: '2017-09-15T10:40:11.000Z', organization: 41, version: 6 }, { employee: 70, month: 4, year: 2017, id: 9, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 7 }, { employee: 70, month: 7, year: 2017, id: 10, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 6 }, { employee: 70, month: 8, year: 2017, id: 11, createdAt: '2017-09-15T10:40:58.000Z', updatedAt: '2017-09-15T10:40:58.000Z', organization: 41, version: 7 }]; const currentMonth = 11; var employeeVersionForMonth = _.head( _.orderBy( _.filter(data, function(o) { return o.month <= currentMonth; }), ['month', 'version'], ['desc', 'desc'])); console.log(employeeVersionForMonth); 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script> 

一个人走:

 const currentMonth = 5 // 0, 5, 10 let fMonth = -Infinity let fVersion = -Infinity let fIndex for (let i = 0; i < data.length; i++) { let item = data[i] if ( (item.month > fMonth && item.month <= currentMonth) || (item.month === fMonth && item.version > fVersion) ) { fMonth = item.month fVersion = item.version fIndex = i } }