javascript - 修改此白名单函数,使其返回具有特定键值字段的对象
问题描述
我正在尝试编写一个白名单实用程序函数,该函数可用于多个不同的帖子和补丁请求。现在,我正试图让它只适用于一种情况。用户发出一个 post 请求,请求返回一个包含多个字段的对象,我们只希望用户能够在请求中创建或编辑某些字段。对于此示例,两个字段是 key1 和 invoiceDate。
我有一个提前定义的数组,在这里它被称为acceptableLine1。此实用方法应该能够将请求正文与可接受字段数组进行比较并确定这是一个 vLineValue,复制请求正文,新正文将只有键 key1 和 invoiceDate 并且它应该填充新对象具有来自用户发送的请求的值的键。
我在检查并返回带有值的新对象时遇到问题。现在,结果对象复制了请求对象中的所有键和请求对象中的所有值,它应该只复制字段 key1 和 invoiceDate,并用请求中的值填充它。
很抱歉解释冗长,希望它清楚,如果需要作出任何澄清,我很乐意进一步解释。
const acceptableLine1 = ['invoiceDate', 'key1']
// body that comes from a bill line post request
let vLineValue = {
id: 19,
invoiceDate: '2000-01-25',
key1: 'abc',
}
export default async (
body,
acceptableFieldsOne,
acceptableFieldsTwo,
acceptableFieldsThree,
// acceptable fields are arrays of acceptable fields for post or patch requests to bills, bill-lines, dist-lines
) => {
// start off with taking the body input and making a copy of it
const whiteListBody = body;
// after making a copy of the body, check each of the arrays, see if the body has keys that match any of the array strings
// get an array of the keys from the body
const whiteListObjectKeys = Object.keys(whiteListBody);
// get an array of the whiteList objects values
const whiteListObjectValues = Object.values(whiteListBody);
// compare whiteListObjectKeys array with each of the acceptableFields arrays
// if the arrays match, return a new object with the keys that match and their original values
// result should only have the keys 'key1' and 'invoiceDate' with the values 'abc', '2000-01-25'
result = Object.fromEntries(
whiteListObjectKeys.map((_, i) => [
whiteListObjectKeys[i],
whiteListObjectValues[i],
])
);
};
解决方案
尝试使用filter()
数组的方法返回所需的条目。使用 将body
对象转换为具有[key, value]
对的数组Object.entries()
,过滤该数组并将条目转换回对象。
此外,您实际上并不需要创建副本,body
但如果您确实Object.assign()
需要制作实际副本。
我创建了...acceptableFields
rest 参数来接受无限数量的acceptableFields
数组。这些数组将组合成一个数组,flat()
过滤器将循环通过该数组。
const acceptableFieldsVoucherLine = ['invoiceDate', 'description']
// body that comes from a voucher line post request
let VoucherLine = {
id: 12,
invoiceDate: '2020-02-27',
description: 'abc',
voucherId: '18',
createdBy: 'some value',
orgServiceDate: null,
paymentDate: null,
processingDate: null,
updatedBy: null,
createdAt: 'some value',
updatedAt: 'some value',
amountCents: 0,
federalAmountCents: 0,
nonFederalAmountCents: 0,
archivedAt: null,
lineNumber: 7
}
export default async (body, ...acceptableFields) => {
// Start off with taking the body input and making a copy of it.
const whiteListBody = Object.assign({}, body);
// Convert object to array with entries: [key, value] pairs.
const whiteListEntries = Object.entries(whiteListBody);
// Combine all arrays into a single array.
const acceptableFieldsList = acceptableFields.flat();
// Function for checking if the key matches an acceptable field.
const whiteListHasEntry = ([key, value]) => acceptableFieldsList.some(field => field === key);
// Filter the entries with the whiteListHasEntry function.
const filteredWhiteListBody = whiteListEntries.filter(whiteListHasEntry);
// Turn the result back into an object.
const result = Object.fromEntries(filteredWhiteListBody);
}
推荐阅读
- python - 从静态字段生成的 Django F 日期表达式
- python - 如何扩展此正则表达式以匹配备选方案和分组?
- javascript - 无法在 React 表单中提交所有数据
- typescript - 确保字符串文字联合属于对象的键
- powerbi - 带有分发列表的 Power BI RLS 不起作用
- c - 使用 OpenSSL 进行高性能 HMAC 计算
- flutter - 在屏幕顶部颤动BottomSheet?
- amazon-ec2 - grafana ec2-instance 按标签过滤
- html - 将脚本生成的 HTML 发送到具有正确表格边距的 Google Doc
- java - 按照创建顺序给定一个所需字符串的数组。由于两个字符串不能相等,因此替换它对它们进行版本控制