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
这里我需要做一个算法来从数组中获取细节,
我想根据所需的月份数从数组中获取详细信息。
如果月份编号与数组中存在的记录匹配,则返回它,如果该月份有多个记录,则它应该通过查找版本的最高值返回月份详细信息,假设在上面的数组中对于第 7 个月,有 2 条版本为 6 和 7 的记录,所以我想要的是最高版本为 7 的对象。
第 7 个月有 2 条记录,所以我会得到
{ 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 }
如果用户提供的月份号不存在,那么它应该寻找离它最近的最低月份,并提供最高版本的对象,但是如果有更多的最低月份号的对象,那么它应该采用最高版本的数据
假设我想要第 5 个月或第 6 个月的记录,但在数组中没有那个月的记录,所以我将寻找壁橱最低的月份,即 4,第 4 个月有多个记录,所以我将过滤我将想要获得具有最高版本 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 }
Based on rules here , 如果提供的月份编号不存在且之前月份没有记录,则提供与提供的月份编号最接近的月份的最高版本的对象。
到目前为止我所尝试的都在这里。
这项工作看起来很简单,但我想我把它变得更复杂了.. 非常感谢任何类型的帮助。感谢。
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)
最佳答案
只需调用以下函数即可根据您的要求获得结果
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));
关于javascript - 具有自定义要求和数据操作的数组过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46272476/