将数组[ "GPPM:SAMPLING:SAMPLINGSETUP", "GPPM:SAMPLING:SAMPLINGSETUP:DELETE", "GPPM:SAMPLING:SAMPLINGSETUP:EXPORT", "GPPM:SAMPLING:SAMPLINGSETUP:HISTORY", "GPPM:SAMPLING:SAMPLINGSETUP:IMPORT", "GPPM:SAMPLING:SAMPLINGSETUP:QUERY", "GPPM:SAMPLING:SAMPLINGSETUP:SETUP", "GPPM:SAMPLING:SAMPLINGSETUP:SWITCH", "GPPM:SAMPLING:SAMPLINGSETUP:VIEW", "GPPM:SAMPLING:SAMPLINGSETUP:TARGETEQ", "GPPM:SAMPLING:SAMPLINGVIEW", "GPPM:SAMPLING:SAMPLINGVIEW:TARGETEQ" ]转化成[ "DELETE", "EXPORT", "HISTORY", "IMPORT", "QUERY", "SETUP", "SWITCH", "VIEW", "TARGETEQ", ]
时间: 2023-07-30 17:11:15 浏览: 185
可以使用JavaScript的字符串处理函数和循环来实现将数组中的每个元素转化为需要的格式。以下是一个示例代码:
```javascript
const array = [
"GPPM:SAMPLING:SAMPLINGSETUP",
"GPPM:SAMPLING:SAMPLINGSETUP:DELETE",
"GPPM:SAMPLING:SAMPLINGSETUP:EXPORT",
"GPPM:SAMPLING:SAMPLINGSETUP:HISTORY",
"GPPM:SAMPLING:SAMPLINGSETUP:IMPORT",
"GPPM:SAMPLING:SAMPLINGSETUP:QUERY",
"GPPM:SAMPLING:SAMPLINGSETUP:SETUP",
"GPPM:SAMPLING:SAMPLINGSETUP:SWITCH",
"GPPM:SAMPLING:SAMPLINGSETUP:VIEW",
"GPPM:SAMPLING:SAMPLINGSETUP:TARGETEQ",
"GPPM:SAMPLING:SAMPLINGVIEW",
"GPPM:SAMPLING:SAMPLINGVIEW:TARGETEQ"
];
const transformedArray = array.map(item => {
const parts = item.split(':');
return parts[parts.length - 1];
});
console.log(transformedArray);
```
这段代码将会输出转换后的数组:
```
[
"SAMPLINGSETUP",
"DELETE",
"EXPORT",
"HISTORY",
"IMPORT",
"QUERY",
"SETUP",
"SWITCH",
"VIEW",
"TARGETEQ",
"SAMPLINGVIEW",
"TARGETEQ"
]
```
在代码中,我们使用 `split(':')` 将每个元素按冒号分割成一个字符串数组,然后取出最后一个元素作为需要的结果。最后,我们使用 `map` 方法将转换后的结果存储在新的数组中。
阅读全文