用Javascript程序分割url参数

在javascript函数下面会对包含重复参数的url参数进行分条,或者如果一个正在传入的可选项目将根据该可选值删除整个url的参数。

function stripUrlParams(url, paramsToStrip){
	
  if(url == null)
	  return
  let arr
  let str = url.split('?')
  if(str[1] != undefined)
	  arr = str[1].split('&')
  else
	  return url
  
  let target = ''
  let already = ''
  let count = 0
  let replace
  let arr2 = []
  
  if(paramsToStrip != null) {
	  for(let j = 0; j < paramsToStrip.length; j++) {
		  
			replace = paramsToStrip[j] + "="
			
			for(let i = 0; i < arr.length; i++) {
				
				if(arr[i].substring(0, 2) == replace) {
					 arr[i] = 'done'
						
				} 
				
			}
				
		  }
		 while(arr.includes('done'))  {
			  
			  if(arr[count] == 'done') {
				  arr.splice(count, 1)
			  } else {
				  count++
			  }
		  }
		 count = 0
  }
 
 for(let i = 0; i < arr.length; i++) {
	  
	  target = arr[i].split('=')[0]
	  
	  for(let j = i + 1; j < arr.length; j++) {
		  next = arr[j].split('=')[0]
		  if(next == target && already != target)
			  arr[j] = 'done'
		  else
			  continue
		  
	  }
	  already = target
}
 
 while(arr.includes('done'))  {
	  
	  if(arr[count] == 'done') {
		  arr.splice(count, 1)
	  } else {
		  count++
	  }
}
  return str[0] + "?" + arr.join('&')

}

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of