如何将特定但相关条目(键值对)的每个值收集到一个数组中,并将后者分配给一个唯一的合并键?
How does one collect each value of specific but related entries (key-value pairs) into a single array and does assign the latter to a sole merged key?
来自数据库结果。我收到了类似
的回复
[
{
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: 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'
}]
},
, {
....},
...........
]
我无法更改数据库,也无法获取响应。我如何做到,而且员工不限于它可以是任何东西。我该怎么做
你可以先得到emp_sk个值的个数,然后用for循环把sk和name插入emp数组,然后按原样插入剩下的键值对
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};
})
)
下一个提供的方法一般解决合并任何对象的特定条目(键值对)的任务,基于自定义可配置规则集,这些规则集绑定到通用合并功能。
因此,人们总是可以像 mergeItemEntriesByBoundConfig
一样重复使用单个函数,并根据需要调整规则。
配置必须包含两个数组,skips
和 takes
。
前者...skips
...将仅包含正则表达式项,每个项都描述应跳过对象的哪个键(因此不处理)。
后者 ... takes
... 专门包含每个描述哪种条目将 合并到 单键 中的项目。因此,这样的项目具有两个属性,regX
和 create
。
正则表达式通过其 key
标识要处理的条目,而 create
是一个函数,它始终必须 return a [=47= 合并键和键值对 ]合并值将被推入合并键所引用的数组。
为了始终以最佳方式完成此创建任务,任何 create
函数都会将以下三个参数传递给 ... [item, key, regX]
... 其中 item
是处理对象本身,key
取自当前处理的条目,regX
是did/does匹配当前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 [
mergerKey,
mergerValue
] = create(item, key, regX);
(merger[mergerKey] ??= []).push(mergerValue);
}
});
}
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 returns a (merger) key-value tuple.
return ['emp', {
id: item[key],
name: item[`emp${ link }name`],
}];
},
regX: /^emp(?<link>\d+)sk$/,
}, {
create: (item, key/*, regX*/) => {
// creates and returns a (merger) 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; }
来自数据库结果。我收到了类似
的回复[
{
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: 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'
}]
},
, {
....},
...........
]
我无法更改数据库,也无法获取响应。我如何做到,而且员工不限于它可以是任何东西。我该怎么做
你可以先得到emp_sk个值的个数,然后用for循环把sk和name插入emp数组,然后按原样插入剩下的键值对
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};
})
)
下一个提供的方法一般解决合并任何对象的特定条目(键值对)的任务,基于自定义可配置规则集,这些规则集绑定到通用合并功能。
因此,人们总是可以像 mergeItemEntriesByBoundConfig
一样重复使用单个函数,并根据需要调整规则。
配置必须包含两个数组,skips
和 takes
。
前者...skips
...将仅包含正则表达式项,每个项都描述应跳过对象的哪个键(因此不处理)。
后者 ... takes
... 专门包含每个描述哪种条目将 合并到 单键 中的项目。因此,这样的项目具有两个属性,regX
和 create
。
正则表达式通过其 key
标识要处理的条目,而 create
是一个函数,它始终必须 return a [=47= 合并键和键值对 ]合并值将被推入合并键所引用的数组。
为了始终以最佳方式完成此创建任务,任何 create
函数都会将以下三个参数传递给 ... [item, key, regX]
... 其中 item
是处理对象本身,key
取自当前处理的条目,regX
是did/does匹配当前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 [
mergerKey,
mergerValue
] = create(item, key, regX);
(merger[mergerKey] ??= []).push(mergerValue);
}
});
}
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 returns a (merger) key-value tuple.
return ['emp', {
id: item[key],
name: item[`emp${ link }name`],
}];
},
regX: /^emp(?<link>\d+)sk$/,
}, {
create: (item, key/*, regX*/) => {
// creates and returns a (merger) 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; }