JavaScript从最后一个字符串中删除分号

jogvjijk  于 2023-05-05  发布在  Java
关注(0)|答案(4)|浏览(148)
const arr = [
    {
        "id": "753311",
        "role": "System Of Record (SOR)",
        "license": "Target",
        "DC": "Client · L2 (Inactive), Account · L1",
        "managedGeography": "North America · L2, International · L2",
        "managedSegment": "Institutional Clients Group [L3], Discontinued Ops [L2]",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "752872",
        "role": "Authorized Redistributor (AR)",
        "license": "Interim",
        "DC": "Holding · L1, Document · L1, Compliance · L1",
        "managedGeography": "Unspecified",
        "managedSegment": "Unspecified",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "752583",
        "role": "Authorized Redistributor (AR)",
        "license": "Target",
        "DC": "Agreement · L1, Asset · L1, Activity · L1, Account · L1",
        "managedGeography": "Unspecified",
        "managedSegment": "Unspecified"
    }
]

let adsList = arr.map(selectedObj => {
        if (selectedObj.checked) {
          return selectedObj.role + ", " + selectedObj.license + ", " + selectedObj.DC + ", " + selectedObj.managedGeography + ", " + selectedObj.managedSegment + ";\n"
        } else {
          return '';
        }
      }).filter((str) => str.length !== 0).join('\n');
      
 console.log(adsList)

嗨,我有一个数组,我基本上返回字符串,如果对象包含检查的属性,并用分号分隔它们,但需要删除最后一个。
我不确定如何从输出中删除unspecified后面的最后一个分号,在这种情况下,任何线索都将非常有用。

s1ag04yj

s1ag04yj1#

不要在map调用中添加分号,但在最后的join调用中将其用作分隔符。
不是问题,但是:

  • 你可以使用Boolean作为过滤器回调函数。
  • .checked为false时,您可以返回.checked,而不是返回'',这允许您返回一个没有if..else的表达式。
  • 可以使用模板字符串来用逗号连接属性。
const arr = [{"id": "753311","role": "System Of Record (SOR)","license": "Target","DC": "Client · L2 (Inactive), Account · L1","managedGeography": "North America · L2, International · L2","managedSegment": "Institutional Clients Group [L3], Discontinued Ops [L2]","checked": true,"checkBoxPatched": true},{"id": "752872","role": "Authorized Redistributor (AR)","license": "Interim","DC": "Holding · L1, Document · L1, Compliance · L1","managedGeography": "Unspecified","managedSegment": "Unspecified","checked": true,"checkBoxPatched": true},{"id": "752583","role": "Authorized Redistributor (AR)","license": "Target","DC": "Agreement · L1, Asset · L1, Activity · L1, Account · L1","managedGeography": "Unspecified", "managedSegment": "Unspecified"}];

let adsList = arr.map(obj =>
    obj.checked && `${obj.role}, ${obj.license}, ${obj.DC}, ${obj.managedGeography}, ${obj.managedSegment}`
).filter(Boolean).join(';\n\n');
      
 console.log(adsList)
cu6pst1q

cu6pst1q2#

您可以更新代码以使用;\n连接数组,如果您希望保留中间的额外行,则使用;\n\n

let adsList = arr.map(selectedObj => {
    if (selectedObj.checked) {
        return selectedObj.role + ", " + selectedObj.license + ", " + selectedObj.DC + ", " + selectedObj.managedGeography + ", " + selectedObj.managedSegment;
    } else {
        return '';
    }
}).filter((str) => str.length > 0).join(';\n');
const arr = [{
    "id": "753311",
    "role": "System Of Record (SOR)",
    "license": "Target",
    "DC": "Client · L2 (Inactive), Account · L1",
    "managedGeography": "North America · L2, International · L2",
    "managedSegment": "Institutional Clients Group [L3], Discontinued Ops [L2]",
    "checked": true,
    "checkBoxPatched": true
  },
  {
    "id": "752872",
    "role": "Authorized Redistributor (AR)",
    "license": "Interim",
    "DC": "Holding · L1, Document · L1, Compliance · L1",
    "managedGeography": "Unspecified",
    "managedSegment": "Unspecified",
    "checked": true,
    "checkBoxPatched": true
  },
  {
    "id": "752583",
    "role": "Authorized Redistributor (AR)",
    "license": "Target",
    "DC": "Agreement · L1, Asset · L1, Activity · L1, Account · L1",
    "managedGeography": "Unspecified",
    "managedSegment": "Unspecified"
  }
]

let adsList = arr.map(selectedObj => {
  if (selectedObj.checked) {
    return selectedObj.role + ", " + selectedObj.license + ", " + selectedObj.DC + ", " + selectedObj.managedGeography + ", " + selectedObj.managedSegment;
  } else {
    return '';
  }
}).filter((str) => str.length > 0).join(';\n\n');

console.log(adsList)
2w2cym1i

2w2cym1i3#

可以使用slice()方法从结果字符串中删除最后一个字符:

let adsList = arr.map(selectedObj => {
  if (selectedObj.checked) {
    return selectedObj.role + ", " + selectedObj.license + ", " + selectedObj.DC + ", " + selectedObj.managedGeography + ", " + selectedObj.managedSegment + ";\n"
  } 
  return '';
}).filter((str) => str.length !== 0).join('\n').slice(0, -2);

console.log(adsList)
vsaztqbk

vsaztqbk4#

可以使用以下正则表达式:

/;(\s|\\n)*$/

若要在字符串末尾找到分号,然后将其替换为空字符串。
正则表达式具有以下含义:
;匹配文字分号
(\s|\\n)*匹配一个或多白色或\n文字
$匹配字符串的结尾

const arr = [
    {
        "id": "753311",
        "role": "System Of Record (SOR)",
        "license": "Target",
        "DC": "Client · L2 (Inactive), Account · L1",
        "managedGeography": "North America · L2, International · L2",
        "managedSegment": "Institutional Clients Group [L3], Discontinued Ops [L2]",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "752872",
        "role": "Authorized Redistributor (AR)",
        "license": "Interim",
        "DC": "Holding · L1, Document · L1, Compliance · L1",
        "managedGeography": "Unspecified",
        "managedSegment": "Unspecified",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "752583",
        "role": "Authorized Redistributor (AR)",
        "license": "Target",
        "DC": "Agreement · L1, Asset · L1, Activity · L1, Account · L1",
        "managedGeography": "Unspecified",
        "managedSegment": "Unspecified"
    }
]

let adsList = arr.map(selectedObj => {
        if (selectedObj.checked) {
          return selectedObj.role + ", " + selectedObj.license + ", " + selectedObj.DC + ", " + selectedObj.managedGeography + ", " + selectedObj.managedSegment + ";\n"
        } else {
          return '';
        }
      }).filter((str) => str.length !== 0).join('\n').replace(/;(\s|\\n)*$/, '');
      
 console.log(adsList)

相关问题