JS in some () and every () and join () and concat () and Pop (), push (), Shift (), Unshfit () and map () and filter ()

Source: Internet
Author: User
Tags pear

One, Array

1, some () and every ()

some () is a specified function that runs on each item in an array, and returns True if the function returns true for either item.

every () is a given function that runs for each item in the array, and returns True if the function returns true for each item.

var array = [1,3,5,7,9,11,13,15,17]undefinedarray.some (function(item,index) {     return item>9})//true

Returns True

var array = [1,3,5,7,9,11,13,15,17]undefinedarray.every (function(item,index) {     return item>9})//false

Returns false

2. Join ()

The join () method is used to put all the elements in an array into a string.

var arr =[4,5,34,3453,3453,33, "haha haha"]; var arr2 = Arr.join (""); Console.log (ARR2); Console.log (typeof//string Console.log (typeof(arr));   // Object //45343453345333 haha haha //string//object

Output:

45343453345333 haha //string//object

3, concat ()

Connect 2 and more arrays, returning a new array. the method does not alter the existing array, but only returns a copy of the concatenated array.

var New Array (3) arr[0] = "George"arr[1] = "John"arr[varnew Array (3) arr2[ 0] = "James"arr2[1] = "Adrew"var array3 = arr.concat (arr2); Console.log (ARRAY3); Console.log (arr); Console.log (ARR2);

Output:

(6) ["George", "John", "Thomas", "James", "Adrew", empty]
(3) ["George", "John", "Thomas"]
(3) ["James", "Adrew", empty]

4, Pop () and push () and shift () and unshift ()

Changes the length and content of the array itself.

The difference is that push (), pop () is added or subtracted from the end of the array,unshift (), and Shift ( ) are added or subtracted from the head of the array.

(1) push () is a number of elements added to the tail, unshift () is to add several elements to the head, returning the new length of the array

var arr = [1, 2];arr.push (3,4); Console.log (arr);    // [1,2,3,4] arr.unshift (0,0.5);  Console.log (arr);   // [0,0.5,1,2,3,4]

(2) Pop () removes 1 elements from the end of the array (delete only 1)and returns the deleted element ; The empty array is continued to delete without error, but returns undefined; Shift () is to remove 1 elements from the head (delete only 1) ...

// connect the above array arrarr.pop ()   // return 4console.log (arr)  ; // [0, 0.5, 1, 2, 3]

Arr.shift () //Return 0
Console.log (arr) //[0.5,1,2,3]

5. Map ()

map"Map", that is, the original array is "mapped" into a corresponding new array.

var data = [1, 2, 3, 4]; var array = Data.map (function  (item) {  return item * item;});
Console.log (array) //[1,4,9,16]

Output: [1,4,9,16]

var users = [  "Zhang Yun", "email": "[email protected]", "Age": "" "},  " Dancing Dogs ",   " email " : "[email protected]", "Age": "  li Xiaolu", "e-mail", "email  ": "[email protected]", "Age": "[]"}]; var emailsage = Users.map (functionreturn {email:item.email,age:item.age}});

Output

[Email: [email protected] ", Age : "}, {Email: "[email protected]", Age : "The"}, {Email: "[email protected]", Age : "+"}]

6. Filter ()

The filter () method is used to Array filter out certain elements, then return the remaining elements and create a new array that contains all the elements passed through the test.
function Isbigenough (Element) {     return element >=;} var = [5, 8, Filtered,].filter (Isbigenough); Console.log (filtered); // [A]

Output: [12,130,44]

Function (1): One Array , delete an even number, keep only odd

var arr = [1, 2, 4, 5, 6, 9, ten,]; var num = Arr.filter (function  (x) {     return x% 2!== 0;});
Console.log (num) //[1,5,9,15]

Function (2): Array erase the empty string in

var null, Undefined, ' C ', '  var string = Arr.filter (function  (s) {      return // Note: The following versions of IE9 do not have the trim () method  //  [' A ', ' B ', ' C ']

Function (3): Array a repeating element that can be subtly removed,

Note: Removing a repeating element relies on indexOf always returning the position of the first element, and subsequent duplicate element positions are indexOf not equal to the returned position and are therefore filter filtered out.

var  r, arr = [' Apple ', ' strawberry ', ' banana ', ' pear ', ' apple ', ' orange ', ' orange ', ' strawberry '= arr.filter (Fun ction  (element, index, self) {    return self.indexof (element) = = = index;}); Console.log (R.tostring ()); Apple,strawberry,banana,pear,orange

JS in some () and every () and join () and concat () and Pop (), push (), Shift (), Unshfit () and map () and filter ()

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.