数组的扩展
数组的扩展
forEach
格式:
//第一个参数为函数
//函数的第一个参数 是遍历的当前元素
//函数的第二个参数 当前元素的下标
//函数的第三个元素 数组本身。
//第二个参数为:函数中this指向。
arr.forEach(function(item,index,arr){
},obj);
var arr = [
"千与千寻",
"肖申克的救赎",
"谁先爱上他的",
"哈尔的移动城堡",
"海上钢琴师"
]
var lis = document.querySelectorAll("li")
arr.forEach(function(item,index,self){
// console.log("电影名:"+item+",下标为:"+index);
this[index].innerHTML = item;
},lis);
map方法
map 映射含义,用法与forEach基本类似。
可以在函数中定义返回值。返回是一个数组。
var arr = [
{
name:"千与千寻",score:"8.6"},
{
name:"肖申克的救赎",score:"9.6"},
{
name:"谁先爱上他的",score:"8.3"},
{
name:"哈尔的移动城堡",score:"8.9"},
{
name:"海上钢琴师",score:"9.0"}
]
var arr2 = arr.map(function(item){
return item.score;
});
console.log(arr2);
Array.from()
Array.from
方法可以将类似数组的对象转为真正的数组。比如:DOM获取的伪数组,arguments对象。
let lis = document.querySelectorAll("li");
console.log(lis);
var arr