美文网首页
2018-01-22/(数组)遍历

2018-01-22/(数组)遍历

作者: 加加大叔 | 来源:发表于2018-01-22 21:56 被阅读0次

一、for in | for of

1)for in

for...in...用于遍历数组或者对象的属性。这属性包括自身的属性和原型链的可枚举的属性!(非整数类型的名称和继承的那些)
该遍历方法返回的先后次序可能会因浏览器(宿主环境)而异;
因此不建议用 for...in...去遍历数组

let arr = [1,2,3];
let obj = {
  name:'JY',
  age:23.5
}
for(let key in arr/obj){
  console.log(key);
  //arr : 0,1,2 (0,1,2是String类型)
 //过滤继承的属性的方法:
   obj.hasOwnProperty(key)

 // obj : name,age
}

2) for...of...

for...of...可以用来遍历有迭代器(iterator)的集合(数组,类数组,Map,Set,string,arguments等),不建议用来遍历对象,遍历对象建议用for...in...(本质工作)

//遍历数组
let arr = [2,3,4];
for(let value of arr){
  console.log(value); // 1,2,3
}
//遍历字符串
let str = 'boo'
for(let value of str){
  console.log(value); // 'b','o','o'
}
...

for...of...方法遍历数组时,可以正确相应break、continue、return语句

二、常用数组遍历方法

1)arr.forEach();

forEach() 方法对数组的每个元素执行一次提供的函数。
forEach()方法不能使用break、continue、return等语句
forEach()方法不会改变原数组。
用法

[1,2,3].forEach(function(value,index,arr){
  console.log(value,index,arr);
  // 1,0,[1,2,3]
  // 2,1,[1,2,3]
  // 3,2,[1,2,3]
},this)
// this,表示函数内的this指向。可以

2)arr.map()

map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
即返回每个元素执行回调的返回值,组成的新数组

var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots的值为[1, 2, 3], numbers的值仍为[1, 4, 9]

3) arr.reduce()

对数组中的所有元素调用指定的回调函数。该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
reduce(function,initialValue)
initialValue 用作第一个调用 callback的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素

//数组里所有值的和
var sum = [0, 1, 2, 3].reduce(function (a, b) {
  return a + b;
}, 0);
// sum is 6


//将二维数组转化为一维
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
  function(a, b) {
    return a.concat(b);
  },
  []
);
// flattened is [0, 1, 2, 3, 4, 5]

4) arr.every()

every()方法测试数组的所有元素是否都通过了指定函数的测试。
只有每个callback函数返回true,整个arr.every()才会返回true,否则false

function isBigEnough(element, index, array) {
  return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true

5) arr.some()

some()方法与every()方法相反,只要子元素中有一个令callback函数返回true,则整个arr.some()返回true;全不满足则返回false;

const isBiggerThan10 = (element, index, array) => {
  return element > 10;
}

[2, 5, 8, 1, 4].some(isBiggerThan10);  
// false

[12, 5, 8, 1, 4].some(isBiggerThan10); 
// true

array.slice()是对数组的深/浅拷贝,不会影响原数组的内部结构
num | 0 向上取整 Math.floor()类似

相关文章

  • 2018-01-22/(数组)遍历

    一、for in | for of 1)for in for...in...用于遍历数组或者对象的属性。这属性包括...

  • angular2foreach遍历的几种用法

    遍历简单的数组 遍历数组对象 遍历嵌套数组

  • VS常用四种遍历数组的方法

    目录 for 遍历数组 for in 遍历数组 for of 遍历数组 forEach遍历数组 优缺点总结原文:h...

  • foreach/forin

    1.for/in遍历属性,数组是遍历下标 for/each遍历属性值,数组遍历数组的值

  • JS数组遍历的三种常用方法

    1.数组下标遍历 数组下标遍历是最常用也最普通的数组遍历方式 例如: 2.for in遍历 for in是根据数组...

  • for_of循环

    for(let value of target){}循环遍历 遍历数组 遍历Set 遍历Map 遍历字符串 遍历伪数组

  • PHP中的数组

    数组分类 索引数组 关联数组 数组遍历 传值遍历 传址遍历 数组函数 指针操作函数 current($array)...

  • Go的数组和指针

    一、 定义数组 二、 遍历数组 下标遍历 range遍历(index) range遍历(index,value) ...

  • forEach、for-in与for-of的区别

    遍历数组推荐for of ,遍历对象推荐 for in for in可以用来便利数组和对象 for in在遍历数组...

  • 6.v-for指令

    1.v-for 遍历数组: 遍历对象数组: 遍历对象: 遍历数据 2.key属性:

网友评论

      本文标题:2018-01-22/(数组)遍历

      本文链接:https://www.haomeiwen.com/subject/ncosoxtx.html