SoFunction
Updated on 2025-03-10

Example analysis of slice function usage example in javascript

This article describes the usage of slice functions in JavaScript. Share it for your reference. The specific analysis is as follows:

The slice function in javaScript, for the slice function of the array object, returns a segment of an array. (still array)
(start, [end]) 

parameter:

arrayObj, a must-have option. An Array object.
start, a must-have option. The start element of the part specified in arrayObj is a subscript calculated from zero.
end, optional. The end element of the part specified in arrayObj is a subscript calculated from zero.

illustrate:

The slice method returns an Array object containing the specified part of arrayObj.
The slice method is copied all the way to the element specified by end, but does not include the element. If start is negative, handle it as length + start, where length is the length of the array. If end is negative, it is treated as length + end, where length is the length of the array. If end is omitted, the slice method will be copied all the way to the end of arrayObj . If end appears before start, no elements are copied into the new array.

Example:

In the following example, except for the last element, all elements in myArray are copied into newArray:

newArray = (0, -1) ----------str's slice

<script type="text/javascript">
function Request(valuename,testurl)
{
var rtnval ;
//Get the current web page address information http://192.168.1.240:85/test/asp/Crmkorea_co_kr/?PARA1=ATEST//var nowAddress = unescape() ;
var nowAddress = testurl
var parameters = new Array() ;
alert((("?")+1, ))
parameters = ((("?")+1, )).split("&") ;
for(var i=0;i<;i++)
{
  alert(i + "--" + parameters[i])
  if(parameters[i].indexOf(valuename) != -1)
  {
  rtnval = parameters[i].split("=")[1] ;
  if(rtnval == undefined || rtnval == null)
  {
   rtnval = "" ;
  }
  return rtnval ;
  }
  else{
  // alert(parameters[0].indexOf(valuename))
  // alert("request must from :" + valuename)
  }
}
return ""
//alert(rtnval) ;
}
var myaddr = "/?para1=test1&PARA1=test2"
alert(Request("PARA1",myaddr))
//Check whether the address contains parameter para1 and return the value of the parameter</script>

I hope this article will be helpful to everyone's JavaScript programming.