将数组[ "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", "GPPM:SABCLING:SAMPLINGVIEW", "GPPM:SABCLING:SAMPLINGVIEW:TARGETEQ", "GPPM:EFGLING:SAMPLINGVIEW", "GPPM:EFGLING:SAMPLINGVIEW:TARGETEQ", ]转化为[ "DELETE", "EXPORT", "HISTORY", "IMPORT", "QUERY", "SETUP", "SWITCH", "VIEW", "TARGETEQ", "TARGETEQ", "TARGETEQ", "TARGETEQ", ]
时间: 2023-08-07 17:13:02 浏览: 117
将数据转换成数组
根据给定的数组,你想要将每个元素中的最后一个冒号后的子字符串提取出来,形成一个新的数组。以下是一个示例代码来实现这个转换:
```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",
"GPPM:SABCLING:SAMPLINGVIEW",
"GPPM:SABCLING:SAMPLINGVIEW:TARGETEQ",
"GPPM:EFGLING:SAMPLINGVIEW",
"GPPM:EFGLING:SAMPLINGVIEW:TARGETEQ"
];
const transformedArray = array.map(item => {
const lastIndex = item.lastIndexOf(':');
return item.slice(lastIndex + 1);
});
console.log(transformedArray);
```
这段代码将会输出转换后的数组:
```
[
"SAMPLINGSETUP",
"DELETE",
"EXPORT",
"HISTORY",
"IMPORT",
"QUERY",
"SETUP",
"SWITCH",
"VIEW",
"TARGETEQ",
"SAMPLINGVIEW",
"TARGETEQ",
"SAMPLINGVIEW",
"TARGETEQ",
"SAMPLINGVIEW",
"TARGETEQ"
]
```
在代码中,我们使用 `lastIndexOf` 方法找到每个元素中最后一个冒号的索引,然后使用 `slice` 方法将其后的子字符串提取出来。最后,我们使用 `map` 方法将转换后的结果存储在新的数组中。
阅读全文