From a database result. I got a response like
[
{
id: 11544,
emp_code: "DF",
date_received: "2000-10-18",
closed_ind: "C",
declined: null,
create_date: "2000-10-18",
billing_rate: null,
emp1sk: 10151,
emp2sk: 10151,
emp3sk: 10052,
emp1name: 'efraim',
emp2name: 'andrea',
emp3name: 'monique',
},
{
....},
...........
]
with this record how I can make a array of object like
[
{
id: 11544,
emp_code: "DF",
date_received: "2000-10-18",
closed_ind: "C",
declined: null,
create_date: "2000-10-18",
billing_rate: null,
emp: [{
id: 10151,
name: 'efraim'
}, {
id: 10151,
name: 'andrea'
}, {
id: 10052,
name: 'monique'
}]
},
, {
....},
...........
]
i can not change the database nor the getting response. how i can make it and also the employee is not limited to the it can be anything. how i can do that
CodePudding user response:
You can first get the count of emp_sk values and then use a for loop to insert sk and name in emp array and insert the rest of key-value pair as it is
const q = [
{
id: 11544,
emp_code: "DF",
date_received: "2000-10-18",
closed_ind: "C",
declined: null,
create_date: "2000-10-18",
billing_rate: null,
emp1sk: 10151,
emp2sk: 10151,
emp3sk: 10052,
emp1name: 'efraim',
emp2name: 'andrea',
emp3name: 'monique'
},
{
id: 11545,
emp_code: "DS",
date_received: "2000-10-18",
closed_ind: "C",
declined: null,
create_date: "2000-10-18",
billing_rate: null,
emp1sk: 10151,
emp2sk: 10151,
emp1name: 'efraim',
emp2name: 'andrea'
}
]
console.log(
q.map((e) => {
const length = Object.keys(e).filter((key) => key.match('emp[0-9] sk')).length;
let emp = [];
for(let i=1; i<=length; i ) {
emp.push({id: e[`emp${i}sk`], name: e[`emp${i}name`]})
}
let ans = {
id: e['id'],
emp_code: e['emp_code'],
date_received: e['date_received'],
closed_ind: e['closed_ind'],
declined: e['declined'],
create_date: e['create_date'],
billing_rate: e['billing_rate']
}
return {...ans, emp};
})
)
CodePudding user response:
The next provided approach generically solves the task of merging specific entries (key-value pairs) of any object, based on custom configurable rule sets, which get bound to the generic merge function.
Thus one can always reuse a single function like mergeItemEntriesByBoundConfig and just adapt the rules to ones needs.
The configuration has to feature two arrays skips and takes.
The former ... skips ... will contain regex items only that each describe which of an object's key should be skipped (thus not processed).
The latter ... takes ... exclusively contains items that each describe which kind of entry will be merged into a single key. Thus such an item features two properties regX and create.
The regular expression identifies the to be processed entry by its key, whereas create is a function which always has to return a key-value pair of the to be merged key and the value that will be pushed into the very array which the merged key does refer to.
In order to achieve this creation task always in the best possible way, any create function gets passed the following three parameters into ... [item, key, regX] ... where item is the processed object itself, and key is taken from the currently processed entry, and regX is the regular expression which did match the current key.
function mergeItemEntriesByBoundConfig(item) {
const { takes, skips } = this;
return Object
.entries(item)
.reduce((merger, [key, value]) => {
let isSkip = skips.some(regX => regX.test(key));
if (!isSkip) {
takes.forEach(({ create, regX }) => {
if (regX.test(key)) {
isSkip = true;
const [
mergedKey,
mergedValue
] = create(item, key, regX);
(merger[mergedKey] ??= []).push(mergedValue);
}
});
}
if (!isSkip) {
// default key-value handling.
merger[key] = value;
}
return merger;
}, {});
}
console.log([{
id: 11544,
emp_code: "DF",
date_received: "2000-10-18",
closed_ind: "C",
declined: null,
create_date: "2000-10-18",
billing_rate: null,
emp1sk: 10151,
emp2sk: 10152,
emp3sk: 10053,
emp1name: 'efraim',
emp2name: 'andrea',
emp3name: 'monique',
}, {
id: 11545,
emp_code: "DS",
date_received: "2000-10-18",
closed_ind: "C",
declined: null,
create_date: "2000-10-18",
billing_rate: null,
emp1sk: 10151,
emp2sk: 10152,
emp1name: 'efraim',
emp2name: 'andrea',
fooX: 'baaaz',
fooYY: 'biiz',
fooZZZ: 'buz',
baaaz: 'bar x',
biiz: 'bar xx',
buz: 'bar xxx',
}].map(mergeItemEntriesByBoundConfig, {
takes: [{
create: (item, key, regX) => {
const { link } = regX.exec(key).groups;
// creates and return a (merged) key-value tuple.
return ['emp', {
id: item[key],
name: item[`emp${ link }name`],
}];
},
regX: /^emp(?<link>\d )sk$/,
}, {
create: (item, key/*, regX*/) => {
// creates and return a (merged) key-value tuple.
return ['foos', item[item[key]]];
},
regX: /^foo\w $/,
}],
skips: [/^emp\d name$/, /b\w z/],
})
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
