Формирование объекта циклом
Уже который час сижу и в голову не приходит как осуществить реализацию объекта такого типа. имея лишь только массив r_arr c объектами, которые выгружаются с базы данных access. Объектов в return может быть не 3, а бесконечно.(столько сколько одинаковых дат в массиве), так же и в plannedReferences: в каждой дате,тоже может быть бесконечное количество объектов. Помогите получить этот return пожалуйста.
r_arr =[
{workShiftDate:'2020-12-18',workShiftStringName: 'Late shift', referenceID:24, plannedQty: 5},
{workShiftDate:'2020-12-18',workShiftStringName: 'Day shift', referenceID:25, plannedQty: 7},
{workShiftDate:'2020-12-19',workShiftStringName: 'Day shift', referenceID:27, plannedQty: 9},
{workShiftDate:'2020-12-19',workShiftStringName: 'Day shift', referenceID:28, plannedQty: 11},
];
//что тут надо сделать?
return[{
workShiftDate: '2020-12-18',
workShiftStringName: 'Late shift',
plannedReferences: [
{
referenceID: 24,
plannedQty: 5
}]
},{
workShiftDate: '2020-12-18',
workShiftStringName: 'Day shift',
plannedReferences: [
{
referenceID: 25,
plannedQty: 7
}]
},{
workShiftDate: '2020-12-19',
workShiftStringName: 'Day shift',
plannedReferences: [
{
referenceID: 27,
plannedQty: 4
},{
referenceID: 28,
plannedQty: 11
}]
}]
Ответы (4 шт):
Автор решения: Aziz Umarov
→ Ссылка
Вот так, как вариант
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];
// 5. Let k be 0.
var k = 0;
// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}
// 7. Return undefined.
return undefined;
},
configurable: true,
writable: true
});
}
let r_arr =[
{workShiftDate:'2020-12-18', referenceID:24, plannedQty: 5},
{workShiftDate:'2020-12-18', referenceID:25, plannedQty: 7},
{workShiftDate:'2020-12-19', referenceID:27, plannedQty: 9},
{workShiftDate:'2020-12-19', referenceID:28, plannedQty: 11},
{workShiftDate:'2020-12-19', referenceID:22, plannedQty: 13},
{workShiftDate:'2020-12-20', referenceID:33, plannedQty: 4},
];
const result = r_arr.reduce(function(acc, value) {
// Group initialization
if (!acc.find(function(item){return item.workShiftDate == value.workShiftDate})) {
acc.push({workShiftDate: value.workShiftDate, plannedReferences: []});
}
// Grouping
acc.find(function(item) { return item.workShiftDate == value.workShiftDate}).plannedReferences.push({
referenceID:value.referenceID, plannedQty: value.referenceID});
return acc;
}, []);
console.log(result);
Автор решения: DiD
→ Ссылка
const r_arr =[
{workShiftDate:'2020-12-18', referenceID:24, plannedQty: 5},
{workShiftDate:'2020-12-18', referenceID:25, plannedQty: 7},
{workShiftDate:'2020-12-19', referenceID:27, plannedQty: 9},
{workShiftDate:'2020-12-19', referenceID:28, plannedQty: 11},
{workShiftDate:'2020-12-19', referenceID:22, plannedQty: 13},
{workShiftDate:'2020-12-20', referenceID:33, plannedQty: 4},
];
console.log([...new Set(r_arr.map(v=>v.workShiftDate))].map(d=>({workShiftDate:d,plannedReferences:r_arr.filter(r=>r.workShiftDate == d).map(r=>({referenceID:r.referenceID,plannedQty:r.plannedQty}))})));
Автор решения: Aziz Umarov
→ Ссылка
Вот ещё вариант, совместимый с IE 9
let r_arr =[
{workShiftDate:'2020-12-18', referenceID:24, plannedQty: 5},
{workShiftDate:'2020-12-18', referenceID:25, plannedQty: 7},
{workShiftDate:'2020-12-19', referenceID:27, plannedQty: 9},
{workShiftDate:'2020-12-19', referenceID:28, plannedQty: 11},
{workShiftDate:'2020-12-19', referenceID:22, plannedQty: 13},
{workShiftDate:'2020-12-20', referenceID:33, plannedQty: 4},
];
const result = r_arr.reduce(function(acc, value) {
// Group initialization
if (acc.every(function(item){return item.workShiftDate != value.workShiftDate})) {
acc.push({workShiftDate: value.workShiftDate, plannedReferences: []});
}
// Grouping
acc.filter(function(item) { return item.workShiftDate == value.workShiftDate})[0].plannedReferences.push({
referenceID:value.referenceID, plannedQty: value.referenceID});
return acc;
}, []);
console.log(result);
Updated.
тут прос100
r_arr =[
{workShiftDate:'2020-12-18',workShiftStringName: 'Late shift', referenceID:24, plannedQty: 5},
{workShiftDate:'2020-12-18',workShiftStringName: 'Day shift', referenceID:25, plannedQty: 7},
{workShiftDate:'2020-12-19',workShiftStringName: 'Day shift', referenceID:27, plannedQty: 9},
{workShiftDate:'2020-12-19',workShiftStringName: 'Day shift', referenceID:28, plannedQty: 11},
];
const result = r_arr.reduce(function(acc, value) {
// Group initialization
if (acc.every(function(item){return item.workShiftDate != value.workShiftDate || item.workShiftStringName != value.workShiftStringName})) {
acc.push({workShiftDate: value.workShiftDate, workShiftStringName: value.workShiftStringName, plannedReferences: []});
}
// Grouping
acc.filter(function(item) { return item.workShiftDate == value.workShiftDate && item.workShiftStringName == value.workShiftStringName})[0].plannedReferences.push({
referenceID:value.referenceID, plannedQty: value.referenceID});
return acc;
}, []);
console.log(result);
Автор решения: Kirill
→ Ссылка
Вот простой и рабочий вариант для IE.
const array = [
{workShiftDate:'2020-12-18',workShiftStringName: 'Day shift 1', referenceID:24, plannedQty: 5},
{workShiftDate:'2020-12-18',workShiftStringName: 'Day shift 2', referenceID:25, plannedQty: 7},
{workShiftDate:'2020-12-19',workShiftStringName: 'Day shift 1', referenceID:27, plannedQty: 9},
{workShiftDate:'2020-12-19',workShiftStringName: 'Day shift 2' , referenceID:28, plannedQty: 11},
{workShiftDate:'2020-12-19',workShiftStringName: 'Day shift 1', referenceID:22, plannedQty: 13},
{workShiftDate:'2020-12-20',workShiftStringName: 'Day shift 2', referenceID:33, plannedQty: 4},
];
function groupByDate(array) {
const pre = {};
array.forEach(function (el) {
const key = el.workShiftDate + '@' + el.workShiftStringName;
if (!pre[key]) {
pre[key] = [];
}
pre[key].push({
referenceID: el.referenceID,
plannedQty: el.plannedQty,
});
});
return Object.keys(pre).map(function (k) {
const keys = k.split('@');
return {
workShiftDate: keys[0],
workShiftStringName: keys[1],
plannedReferences: pre[k],
};
});
}
console.log(groupByDate(array));