The JavaScript Array object

Array object

  Before already know the definition Array (array) and basic operations. The basic operation of the array .

  Let's learn more ways.

Detecting whether an object is an array

instanceof // see if the variable is an instance of the object 
Array.isArray (variable names / parameters) // method provided in HTML5, there are compatibility issues

  Note : instanceof usage

Variable names instanceof constructor

  instanceof array is not limited, it may be used to detect other objects, the use of the same.

Output array

toString () // the array into a string, each separated by a comma 

valueOf () // Returns an array of object itself

Common method

  1, stack operations (last out)

push () // add one or more elements to the end of the array, and returns the new length of the array. 
pop () // remove the last item in the array, and returns the value of the element, modify the length property

  2, the operation queue (FIFO)

push () // add one or more elements to the end of the array, and returns the length of the array 
shift () // take the first element in the array, the length property modification 
unshift () // prepend array item returned array length

  3, sorting method

reverse () // inverted array 
sort () // default sort order at the time of converting an element to a string, and then compare them UTF-16 code units constructed value sequence

  4, the operation method

concat (parameter) // The parameters spliced to the current array 
Slice (begin, end) // the current array taken a new array, does not affect the original array (including begin, not including the end)
splice () // remove or replace an existing element in situ or adding new elements to modify the array, and returns the contents to be modified in an array. This method changes the original array.

  5, the location method

indexof () // return the first index to find a given element in the array, and if not, returns -1. 
lastindexof () // Returns the index of the specified element in the array of the last, if not present or -1. Find a forward from the back of the array,

  6, iterative method (method does not modify the original array in HTML5)

every () // all elements in an array test whether a given function can pass a test. It returns a Boolean value. (If you receive an empty array, this method will return in all circumstances  true.) 
Filter () // create a new array, which contains all the elements to achieve by testing the function of the offer. 
forEach () // function for each element of the array to perform a provided 
map () // create a new array, the result is returned after the results of each element in the array to call a function provided. 
some () // test if there is at least one element function can be provided by the method. This method returns a Boolean value.

  7, is connected to a string method

join (parameter) // all elements of the array are connected to a parameter string, if there is no parameter, comma-connected

  8, an array of empty method

arr = []; // recommended 

arr.length = 0; 

arr.splice (0, arr.length);

 Case:

  1, the output is an array of strings | divided form. As 'a | b | c | d'. There are two ways to achieve.

 1 // 方式一
 2   var array = [a,b,c,d,e,f];
 3   console.log(array.join('|'));
 4 // 方式二
 5 function myJoin(array, seperator) {
 6   seperator = seperator || ',';
 7   array = array || [];
 8   if (array.length == 0){
 9     return '';
10   }
11   var str = array[0];
12   for (var i = 1; i < array.length; i++) {
13     str += seperator + array[i];
14   }
15   return str;
16 }
17 var array = [6, 3, 5, 6, 7, 8, 0];
18 console.log(myJoin(array, '-'));

 

  2、将一个字符串数组的元素的顺序进行翻转。使用两种方式。

 1 // 方式一
 2   var array = ['a','b','c','e','f'];
 3   console.log(array.reverse());
 4 // 方式二
 5 function myReverse(arr) {
 6   if (!arr || arr.length == 0) {
 7     return [];
 8   }
 9   for (var i = 0; i < arr.length / 2; i++) {
10     var tmp = arr[i];
11     arr[i] = arr[this.length - i - 1];
12     arr[arr.length - i - 1] = tmp;
13   }
14   return arr;
15 }
16 
17 var array = ['a', 'b', 'c'];
18 console.log(myReverse(array));

 

  3、工资的数组[1500, 1200, 2000, 2100, 1800],把工资超过2000的删除。

 1 // 方式一
 2 var array =  [1500,1200,2000,2100,1800];
 3 var tmpArray = [];
 4 for (var i = 0; i < array.length; i++) {
 5   if(array[i] < 2000) {
 6     tmpArray.push(array[i]);
 7   }
 8 }
 9 console.log(tmpArray);
10 // 方式二
11 var array =  [1500, 1200, 2000, 2100, 1800];
12 array = array.filter(function (item, index) {
13   if (item < 2000) {
14     return true;
15   }
16   return false;
17 });
18 console.log(array);

 

  4、["c", "a", "z", "a", "x", "a"]找到数组中每一个a出现的位置。

1 var array =  ['c', 'a', 'z', 'a', 'x', 'a'];
2 var  index = -1;
3 do {
4   index = array.indexOf('a',index + 1);
5   if (index !== -1){
6     console.log(index);
7   }
8 } while (index > 0);

 

  5、编写一个方法去掉一个数组的重复元素。

 1 function clear(arr) {
 2       // 1 如何获取数组中每一个元素出现的次数
 3       var o = {}; // 记录数组中元素出现的次数
 4       for (var i = 0; i < arr.length; i++) {
 5         var item = arr[i]; // 数组中的每一个元素
 6         // o[item] = 1;
 7         // 判断o对象是否有当前遍历到的属性
 8         if (o[item]) {
 9           // 如果o[item] 存在,说明次数不为1
10           o[item]++;
11         } else {
12           // 如果o[item] 不存在,说明是第一次出现
13           o[item] = 1;
14         }
15       }
16       // console.log(o);
17 
18       // 2 生成一个新的数组,存储不重复的元素
19       var newArray = [];
20       // 遍历对象o中的所有属性
21       for (var key in o) {
22         // 判断o对象中当前属性的值是否为 1  如果为1 说明不重复直接放到新数组中
23         if (o[key] === 1) {
24           newArray.push(key);
25         } else {
26           // o对象中当前属性 次数不为1 ,说明有重复的,如果有重复的话,只存储一次
27           // 判断当前的newArray数组中是否已经有该元素  
28           if (newArray.indexOf(key) === -1) {
29             newArray.push(key);
30           }
31         }
32       }
33       return newArray;
34     } 
35 
36     var array = ['c', 'a', 'z', 'a', 'x', 'a'];
37     var newArray = clear(array);
38     console.log(newArray);

  6、sort排序。

 1 var arr = [25, 10, 108, 18];
 2     // 默认情况下的sort是对字符编码 从小到大排序
 3 arr.sort();
 4 console.log(arr);
 5 
 6 // 除了默认排序外,还可以指定比较器
 7 function compare1(a, b) {
 8   return a - b;
 9 }
10 function compare2(a, b) {
11   return b - a;
12 }
13 arr.sort(compare1);
14 arr.sort(compart2);
15 
16 // 还可以直接指定比较器
17 arr.sort(function (a,b) {
18    return a - b; 
19 })

 

  7、模拟 sort 内部实现。

 1 // 对数组排序,从小到大  -- 冒泡排序
 2     function sort(array, fnCompare) {
 3       // 外层循环 控制趟数
 4       for (var i = 0; i < array.length - 1; i++) {
 5         // 假设排好序了
 6         var isSort = true;
 7         // 内层循环 控制比较的次数
 8         for (var j = 0; j < array.length - 1 - i; j++) {
 9 
10           if (fnCompare(array[j], array[j + 1]) > 0) {
11             isSort = false;
12             // 交换位置
13             var tmp = array[j];
14             array[j] = array[j + 1];
15             array[j + 1] = tmp;
16           }
17         }
18         // 判断是否排好了
19         if (isSort) {
20           break;
21         }
22       }
23     } 
24 
25     var arr = [56, 10, 1, 17];
26     
27 
28     sort(arr, function (a, b) {
29       return b - a;
30     })
31 
32     console.log(arr);

 

 

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11367406.html