<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array对象 </title>
<script type="text/javascript">
var myarr1= ["我","爱","你"];
document.write(myarr.slice(myarr1.indexOf("爱"),myarr1.length-1));
</script>
</head>
<body>
</body>
</html>
myarr.slice这里是myarr1
myarr.slice这里是myarr1
正确答案:
var myarr1= ["我","爱","你"];
document.write(myarr1.slice(myarr1.indexOf("爱"),myarr1.length));
你的错误:
document.write(myarr.slice(myarr1.indexOf("爱"),myarr1.length-1));
myarr——>myarr1,你打错了
由于arrayObject.slice(start,end)的用法如下:
返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。
故,myarr1.length-1——>myarr1.length
myarr.slice应该是myarr1.slice
错误1:变量名错误myarr,应该是myarr1
错误2:myarr1.length-1不用减1,因为 slice 是 [start ,end)
myarr.slice少了1,应该是myarr1.slice
indexOf()是用在字符串里的,而slice()是用在数组里的