JS Loop traversal

Source: Internet
Author: User

1 map

返回一个新的数组,每个元素为调用func的结果, 不改变原数组

Let list = [1, 2, 3, 4, 5];

let other = List.map((d, index) = = { return d * 2;});

2 Filter

返回一个新的数组,每个元素为调用func的结果, 不改变原数组

let list = [ 1, 2, 3, 4, 5];

let other = List.filter ((d, i) = = {return D% 2;});

3 foreach

没有返回值,只针对每个元素调用func, 不改变原数组缺点:无法使用break,return等终止循环。
let list = [1, 2, 3, 4, 5];let other = [];list.forEach((d, i) => { other.push(d * 2);});

4 for in

for in也可以循环数组,但是不推荐这样使用,for–in是用来循环带有字符串key的对象的方法。缺点:只能获得对象的键名,不能直接获取键值。

5 for

For ordinary objects, thefor...in Loop can traverse the key name, and theFor...of Loop will error.

for of为ES6提供,具有iterator接口,就可以用for of循环遍历它的成员。也就是说,for of循环内部调用的是数据结构的Symbol.iterator方法。for of循环可以使用的范围包括数组、Set和Map结构、某些类似数组的对象(比如arguments对象、DOM NodeList对象)、后文的Generator对象,以及字符串。有些数据结构是在现有数据结构的基础上,计算生成的。
可以遍历 字符串 数组 对象
 

JS Loop traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.