批改状态:合格
老师批语:
<!DOCTYPE html><html><head><title>js数组方法</title></head><body><script>var colors=['red','green','blue','yellow','white','black']var arr = [9,8,77,16,54,4,3,2,1]// toString()把数组转换为字符串//var arr1=arr.toString()//document.write(typeof (arr1))//join()将数组元素结合为一个字符串 并以指定分隔符隔开//document.write(arr.join("-"))//pop()从数组中删除最后一个元素//document.write(colors.pop())//document.write(colors)//push() 方法(在数组结尾处)向数组添加一个新的元素//document.write(colors.push('orange'))//值为新数组的长度//document.write(colors)//shift() 方法删除首个数组元素 删除后不占用空间,返回值为被移除的字符串//document.write(colors.shift())//document.write(colors.length)//unshift() 像数组开头添加新元素 返回值为新数组长度//document.write(colors.unshift('orange'))//document.write(colors)//splice(2,0,'orange','pink')向数组添加新项//[2]代表下标//0代表要删除多少个元素//其余参数为定义要新添加的元素//var newcolors=colors.splice(3,1,'orange','pink')//document.write(newcolors)//concat()连接现有数组创建一个新数组 可以连接多个数组//document.write(colors.concat(arr))//slice(start,end) 截取某部分数组(包含start但不包含end)//document.write(colors.slice(1,4))//sort() 数组排序 以字母顺序对数组进行排序//升序// function up(a,b){// return a-b// }// document.write(arr.sort(up))//降序// function down(a,b) {// return b-a// }// document.write(arr.sort(down))//以随机顺序排序// function rand(a,b){// return 0.5-Math.random()// }// document.write(arr.sort(rand))//forEach为每一个数组元素调用一次函数// var sum=''// arr.forEach(add)// function add(a){// sum= sum+a+'<br>'// }// document.write(sum)//map()方法通过对每个数组元素执行函数来创建新数组。 不会对没有值的数组元素执行函数 不会更改原始数组//var cf=arr.map(fn)//function fn(a){// return a*2// }// document.write(cf)// filter() 方法创建一个包含通过测试的数组元素的新数组。// var xx=arr.filter(fn)//function fn(a) {// return a>=10// }// document.write(xx)// reduce() 方法在每个数组元素上运行函数,以生成(减少它)单个值。从左到右运行 不会减少原始数组 (不懂)//every() 方法检查所有数组值是否通过测试。 值为布尔型 只要有一个不符合就为假false// var xx=arr.every(fn)// function fn(a) {// return a>=10// }// document.write(xx)//some() 方法检查某些数组值是否通过了测试。 值为布尔型 只要有一个符合就为假ture 同上//indexOf() 方法在数组中搜索元素值并返回其索引位置//document.write(colors.indexOf("black"))// document.write(colors.lastIndexOf('black'))//find() 方法返回通过测试函数的第一个数组元素的值。// var bb= arr.find(fn)// function fn(a) {// return a >= 10// }// document.write(bb)// //findIndex() 方法返回通过测试函数的第一个数组元素的索引。// var bb= arr.findIndex(fn1)// function fn1(a) {// return a >= 10// }// document.write(bb)// JsoN格式// var obj={'name':'xh',// 'age':26,// 'sex':'male'// }// document.write(obj.name)</script></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号