var nodeLists = ["/data1/chenleileitest/1T/"]如何快速将上述数组变换成如下的格式["/data1/","/data1/chenleileitest/","/data1/chenleileitest/1T/"]我想的是使用split对数组元素划分,然后再拼接,但这是对数组元素只有一个的情况比较好处理,如果数组的元素比较多的情况呢?["/data1/chenleileitest/1T/","/boot/gurb2/themes/system/","/sys/dev/block/8:16/device/"]将其转换成如下形式["/data1/","/data1/chenleileitest/","/data1/chenleileitest/1T/","/boot/","/boot/gurb2/","/boot/gurb2/themes/","/boot/gurb2/themes/system/","/sys/","/sys/dev/","/sys/dev/block/","/sys/dev/block/8:16/","/sys/dev/block/8:16/device/",]var nodeList = [];nodeList = nodeLists.map(function(value, index, array) { return '/'+value }).filter(function(item,index, array){ return item !== '/' })得到的结果是:["/data1", "/chenleileitest", "/1T"]实现想要的结果需要,第一的加上第二个,第一个加第二个加第三个,组成最终想要的结果
1 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
思路很简单,对数组中的某一项,都先进行拆分,然后每拼接一个路径,就存储一次,直到路径拼接完成
var arr = ["/data1/chenleileitest/1T/","/boot/gurb2/themes/system/","/sys/dev/block/8:16/device/"];
function ss(arrs){
let result = [];
// 数组中的每个路径
arrs.map((item)=>{
let s = '/',
res = [];
item.split('/').map((_)=>{
if(_){
// 拼接路径,并把每次拼接完成的路径存储到res中
s += _+'/';
res.push(s);
}
});
// 把数组中每个拼接的路径都存储到总数组中
result = result.concat(res);
});
return result;
}
ss(arr);
添加回答
举报
0/150
提交
取消