英文:
make list of const and its value using for loop js
问题
for (let arg in this.arguments) {
eval(`const ${arg}s = (listItem.${arg} === null) ? listItem.${arg} = 'empty' : String(listItem.${arg}).toLowerCase();`);
}
英文:
I want to generate these line of codes by using for loop
const clientAppProcessFlowIds = (listItem.clientAppProcessFlowId===null)?listItem.clientAppProcessFlowId='empty':String(listItem.clientAppProcessFlowId).toLowerCase();
const clientAppRoleIds = (listItem.clientAppRoleId ===null)?listItem.clientAppRoleId='empty':String(listItem.clientAppRoleId).toLowerCase()
const clientAppUserIds = (listItem.clientAppUserId ===null)?listItem.clientAppUserId='empty':String(listItem.clientAppUserId).toLowerCase()
const isApprovers = (listItem.isApprover ===null)?listItem.isApprover='empty':String(listItem.isApprover).toLowerCase()
const isInCharges = (listItem.isInCharge ===null)?listItem.isInCharge='empty':String(listItem.isInCharge).toLowerCase()
const isReferers = (listItem.isReferer ===null)?listItem.isReferer='empty':String(listItem.isReferer).toLowerCase()
const prioritys = (listItem.priority ===null)?listItem.priority='empty':String(listItem.priority).toLowerCase()
const actives = (listItem.active ===null)?listItem.active='empty':String(listItem.active).toLowerCase()
const createdAts = (listItem.createdAt ===null)?listItem.createdAt='empty':String(listItem.createdAt).toLowerCase()
const isDeleteds = (listItem.isDeleted ===null)?listItem.isDeleted='empty':String(listItem.isDeleted).toLowerCase()
const clientAppProcessFlows = (listItem.clientAppProcessFlow ===null)?listItem.clientAppProcessFlow='empty':String(listItem.clientAppProcessFlow).toLowerCase()
argList contain :
{
id: '',
clientAppProcessFlowId: '',
clientAppRoleId: '',
clientAppUserId: '',
isApprover: '',
isInCharge: '',
isReferer: '',
priority: '',
active: '',
createdAt: '',
isDeleted: '',
clientAppProcessFlow: '',
}
and listItem
contain objects like :
{
"id": 94,
"clientAppProcessFlowId": 6,
"clientAppRoleId": 542,
"clientAppUserId": 452,
"isApprover": true,
"isInCharge": true,
"isReferer": false,
"priority": 99999999,
"active": true,
"createdAt": "2023-06-25T13:36:23.69",
"isDeleted": false,
"clientAppProcessFlow": null
}
I tried for like :
for(let arg in this.arguments){
eval('const ' + arg+'s' + '= ' + (listItem.arg===null)?listItem.arg='"empty"':String(listItem.arg).toLowerCase() + ';')
}
but it didn't work and its return me the last variable value
答案1
得分: 0
以下是翻译好的部分:
// 检查以下代码片段。它实现了你想要做的事情。
const arguments = {
id: '',
clientAppProcessFlowId: '',
clientAppRoleId: '',
clientAppUserId: '',
isApprover: '',
isInCharge: '',
isReferer: '',
priority: '',
active: '',
createdAt: '',
isDeleted: '',
clientAppProcessFlow: '',
}
const listItem = {
"id": 94,
"clientAppProcessFlowId": 6,
"clientAppRoleId": 542,
"clientAppUserId": 452,
"isApprover": true,
"isInCharge": true,
"isReferer": false,
"priority": 99999999,
"active": true,
"createdAt": "2023-06-25T13:36:23.69",
"isDeleted": false,
"clientAppProcessFlow": null
}
for (let arg in arguments) {
const stmt = 'var ' + arg + 's' + ' = "' + (listItem[arg] === null ? 'empty' : String(listItem[arg]).toLowerCase()) + '";';
eval?.(stmt);
}
console.log({
ids,
clientAppProcessFlowIds,
clientAppRoleIds
});
注意:我已将代码段翻译成中文,但保留了原始的JavaScript代码和标记。
英文:
Check out following snippet. It does what you want to do.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arguments = {
id: '',
clientAppProcessFlowId: '',
clientAppRoleId: '',
clientAppUserId: '',
isApprover: '',
isInCharge: '',
isReferer: '',
priority: '',
active: '',
createdAt: '',
isDeleted: '',
clientAppProcessFlow: '',
}
const listItem = {
"id": 94,
"clientAppProcessFlowId": 6,
"clientAppRoleId": 542,
"clientAppUserId": 452,
"isApprover": true,
"isInCharge": true,
"isReferer": false,
"priority": 99999999,
"active": true,
"createdAt": "2023-06-25T13:36:23.69",
"isDeleted": false,
"clientAppProcessFlow": null
}
for (let arg in arguments) {
const stmt = 'var ' + arg + 's' + ' = "' + (listItem[arg] === null ? 'empty' : String(listItem[arg]).toLowerCase()) + '";'
eval?.(stmt);
}
console.log({
ids,
clientAppProcessFlowIds,
clientAppRoleIds
});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论