关于javascript 取文件夹最短不重复资料

发布于 2021-11-26 01:45:47 字数 276 浏览 884 评论 6

问题如标题,


资料如下:


var my_arr = ["/aaa/library","/library/a1", "/library/b2", "/library", "/library/bc", "/aaa/library1"];


如何筛选后得到


/library
/aaa/library
/aaa/library1
的结果呢?

thanks!


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

緦唸λ蓇 2021-11-27 22:51:08

引用来自“Bruce_Wu”的评论

引用来自“MixedFlow”的评论

写的比较匆忙,可能还要多测测

var my_arr = ["/aaa/library","/library/a1", "/library/b2", "/library","/library", "/library/bc", "/aaa/library1"];
		
var filterPath = function(arr){
	//sort by the num of '/' in path
	arr.sort(function(a,b){
		var aSlashNum = (a.match(///g) || []).length;
		var bSlashNum = (b.match(///g) || []).length;
		if(aSlashNum === bSlashNum){
			return a.length - b.length;
		}
		return aSlashNum - bSlashNum;
	});
	
	//console.log(arr);
	
	var parentPaths = [];
	for(var i = 0; i < arr.length; i++){
		var cur = arr[i];
		//the root path
		if(cur.lastIndexOf('/') === 0 ){
			if(parentPaths.indexOf(cur) === -1)
				//save the new
				parentPaths.push(cur);
				
			continue;
		}
		
		
		var path = cur.substring(0, cur.lastIndexOf('/'));
		//if the path doesn't contains path in parentPaths;
		if(parentPaths.indexOf(path) === -1){
			parentPaths.push(cur);
		}
		
	}
	
	// may need another sort; I'm not sure
	/*parentPaths.sort(function(a,b){
		return a.length - b.length;
	});
	*/
	return parentPaths;
	
};

var result = filterPath(my_arr);
//print
console.log(result);

不乱于心 2021-11-27 22:40:57

引用来自“MixedFlow”的评论

写的比较匆忙,可能还要多测测

var my_arr = ["/aaa/library","/library/a1", "/library/b2", "/library","/library", "/library/bc", "/aaa/library1"];
		
var filterPath = function(arr){
	//sort by the num of '/' in path
	arr.sort(function(a,b){
		var aSlashNum = (a.match(///g) || []).length;
		var bSlashNum = (b.match(///g) || []).length;
		if(aSlashNum === bSlashNum){
			return a.length - b.length;
		}
		return aSlashNum - bSlashNum;
	});
	
	//console.log(arr);
	
	var parentPaths = [];
	for(var i = 0; i < arr.length; i++){
		var cur = arr[i];
		//the root path
		if(cur.lastIndexOf('/') === 0 ){
			if(parentPaths.indexOf(cur) === -1)
				//save the new
				parentPaths.push(cur);
				
			continue;
		}
		
		
		var path = cur.substring(0, cur.lastIndexOf('/'));
		//if the path doesn't contains path in parentPaths;
		if(parentPaths.indexOf(path) === -1){
			parentPaths.push(cur);
		}
		
	}
	
	// may need another sort; I'm not sure
	/*parentPaths.sort(function(a,b){
		return a.length - b.length;
	});
	*/
	return parentPaths;
	
};

var result = filterPath(my_arr);
//print
console.log(result);

緦唸λ蓇 2021-11-27 22:09:09

写的比较匆忙,可能还要多测测

var my_arr = ["/aaa/library","/library/a1", "/library/b2", "/library","/library", "/library/bc", "/aaa/library1"];
		
var filterPath = function(arr){
	//sort by the num of '/' in path
	arr.sort(function(a,b){
		var aSlashNum = (a.match(///g) || []).length;
		var bSlashNum = (b.match(///g) || []).length;
		if(aSlashNum === bSlashNum){
			return a.length - b.length;
		}
		return aSlashNum - bSlashNum;
	});
	
	//console.log(arr);
	
	var parentPaths = [];
	for(var i = 0; i < arr.length; i++){
		var cur = arr[i];
		//the root path
		if(cur.lastIndexOf('/') === 0 ){
			if(parentPaths.indexOf(cur) === -1)
				//save the new
				parentPaths.push(cur);
				
			continue;
		}
		
		
		var path = cur.substring(0, cur.lastIndexOf('/'));
		//if the path doesn't contains path in parentPaths;
		if(parentPaths.indexOf(path) === -1){
			parentPaths.push(cur);
		}
		
	}
	
	// may need another sort; I'm not sure
	/*parentPaths.sort(function(a,b){
		return a.length - b.length;
	});
	*/
	return parentPaths;
	
};

var result = filterPath(my_arr);
//print
console.log(result);

陌若浮生 2021-11-27 21:07:43

不知道,js玩的不好

琴流音 2021-11-27 20:10:24

引用来自“杨金焕”的评论

看不出以上筛选后结果的规律。

也不明白所表达。

琴流音 2021-11-27 08:22:03

看不出以上筛选后结果的规律。

也不明白所表达。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文