生成使用 JavaScript for 循环的常量列表及其值。

huangapple go评论160阅读模式
英文:

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: &#39;&#39;,
  clientAppProcessFlowId: &#39;&#39;,
  clientAppRoleId: &#39;&#39;,
  clientAppUserId: &#39;&#39;,
  isApprover: &#39;&#39;,
  isInCharge: &#39;&#39;,
  isReferer: &#39;&#39;,
  priority: &#39;&#39;,
  active: &#39;&#39;,
  createdAt: &#39;&#39;,
  isDeleted: &#39;&#39;,
  clientAppProcessFlow: &#39;&#39;,
}

const listItem = {
  &quot;id&quot;: 94,
  &quot;clientAppProcessFlowId&quot;: 6,
  &quot;clientAppRoleId&quot;: 542,
  &quot;clientAppUserId&quot;: 452,
  &quot;isApprover&quot;: true,
  &quot;isInCharge&quot;: true,
  &quot;isReferer&quot;: false,
  &quot;priority&quot;: 99999999,
  &quot;active&quot;: true,
  &quot;createdAt&quot;: &quot;2023-06-25T13:36:23.69&quot;,
  &quot;isDeleted&quot;: false,
  &quot;clientAppProcessFlow&quot;: null
}

for (let arg in arguments) {
  const stmt = &#39;var &#39; + arg + &#39;s&#39; + &#39; = &quot;&#39; + (listItem[arg] === null ? &#39;empty&#39; : String(listItem[arg]).toLowerCase()) + &#39;&quot;;&#39;
  eval?.(stmt);
}
console.log({
  ids,
  clientAppProcessFlowIds,
  clientAppRoleIds
});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月27日 17:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定