ES6 Study Notes (1) array (top)

Source: Internet
Author: User

Recently, the company has nothing to do, our boss let me see Es6, Xiao Ying practice the next array of methods, today first to share a part. hehe, I hope we can help you.

Every method:

Overview: every() Method tests whether all elements of an array pass the test of the specified function.

Parameters:

callback : a function to test each element;

element : The element that is currently traversed.

index:当前遍历到的索引。

array : the array itself.

thisArg : callback The value to use when executing this .

Describe:

Xiao Ying's understanding is this: the every method executes the function once for each element in the array callback , 如果当前元素不满足callback 函数中的判断条件则立即 returning false. And will not execute the every method again; 如果该数组中有所有元素都满足callback 函数中的判断条件则立即 return true . callbackwill only be called for indexes that have already been assigned values. are not called for indexes that have been deleted or have never been assigned a value. ( Note: The every original array is not changed.) )

Example:

     Let _everydata1=[1,3,5,10,12].every (function(element, index, array) {         console.log (' current element ' + Element+ ', index: ' +index+ ', current array: ' +array ';          return (element >=);      });     Console.log (_EVERYDATA1);      Let _everydata2=[' 1 ', ' 3 ', ' 5 ', ' Ten ', '].every ' (function(element, index, array) {         Console.log (' current element ' +element+ ', index: ' +index+ ', current array: ' +array ')         ; return (element >= 1);      });      Console.log (_EVERYDATA2);

Printing results:

_everydata1 is false because the first element of the array [1,3,5,10,12] does not satisfy a condition greater than ten (element >=), so the return result is False,index is 0, Stop executing the Every method immediately because it encounters an element that does not meet the criteria. and in [1,3,5,10,12],[' 1 ', ' 3 ', ' 5 ', ' 10 ', ' 12 '] when these two arrays call every , the elements in both arrays can be compared to numbers.

filterMethod:

Overview: filter() method tests all the elements with the specified function and creates a new array that contains all the elements passed by the test.

Parameters:

callback : a function used to test each element, using parameters (element, index, array) when invoked. True indicates that the element is retained (passed test), false is not preserved.;

element : The element that is currently traversed.

index:当前遍历到的索引。

array : the array itself.

thisArg : Optional. callbackthe value to use when executing this .

Describe:

filter The function is called once for each element in the array callback , and callback a new array is created with all elements that return true or value equivalent to true . are callback called only on indexes that have already been assigned, and are not called for those that have been deleted or have never been assigned a value. Those elements that are not callback tested will be skipped and will not be included in the new array. ( Note: filter The original array is not changed.) )

Example:

      function Isbigenough (Element) {        return element >=;      }       = [5, 8,].filter (Isbigenough);      Console.log (filtered); // [A]
find Method:

Overview: if an element in the array satisfies the ,find()  test Condition method, the value of that element is returned, and if no element satisfies the condition, undefined is returned.

Parameters:

callback : a function used to test each element, using parameters (element, index, array) when invoked. True indicates that the element is retained (passed test), false is not preserved.;

element : The element that is currently traversed.

index:当前遍历到的索引。

array : the array itself.

thisArg : Optional. callbackthe value to use when executing this .

Describe:

The Find method executes the function once for each element in the array callback , until there is one callback返回true。当找到了这样一个元素后,该方法会立即返回这个元素的值,否则返回undefined。注意callback函数只会在分配了值的数组索引上调用,而不会在已删除或未分配值的索引上调用。 ( Note: The original array is not changed. )

Example:

     Let _agedata=[{age:20,name: ' Meow a microphone '},{age:18,name: ' Big ying '},{age:23,name: ' Hu hai '}];       function FindName (fruit) {         return fruit.name = = = ' Big ying ';      }       // [{{age:18,name: ' Big Ying '}]      console.log (_agedata);   [{age:20,name: ' Meow '},{age:18,name: ' Big ying '},{age:23,name: ' Hu Hai '}]

FindIndex Method:

Overview: findIndex() method is used to find the index of a specified element in the array, or 1 if the specified element is not found.

Parameters:

callback : a function used to test each element, using parameters (element, index, array) when invoked. True indicates that the element is retained (passed test), false is not preserved.;

element : The element that is currently traversed.

index:当前遍历到的索引。

array : the array itself.

thisArg : Optional. callbackthe value to use when executing this .

Describe:

findIndex() 方法对数组中的每一个元素执行一次回调函数直至有一个回调函数返回真值 。如果找到了一个这样的元素, 则 findIndex 将会立刻返回这个元素的索引。否则 findIndex 将会返回 -1。回调函数只有在数组的索引被分配值的时候才会被调用,若是索引被删除或者没有被赋值将不会被调用。( Note: FindIndex does not modify the called array)

Example:

    function IsPrime (element, index, array) {        return element>4;      }      Console.log ([///  1     function  isprime (element, index, array)        { return element>=4;      }      Console.log ([//  0

ES6 Study Notes (1) array (top)

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.