首页 > 代码库 > JS - 有序数列

JS - 有序数列

************************

作者:张启卫

时间: 2017年6月3号

功能:

 

* 介绍以索引进行排序的数据集合。

 

************************

 

1. 数组对象

1.1 创建数组

var arr = new Array(element0, element1, ..., elementN);var arr = Array(element0, element1, ..., elementN);var arr = [element0, element1, ..., elementN];// 译者注: var arr=[4] 和 var arr=new Array(4)是不等效的,// 后者4指数组长度,所以使用字面值(literal)的方式应该不仅仅是便捷,同时也不易踩坑

 

 

var arr = new Array(arrayLength);var arr = Array(arrayLength);// 这样有同样的效果var arr = [];arr.length = arrayLength;

 

 

数组作为属性赋值给对象

var obj = {};// ...obj.prop = [element0, element1, ..., elementN];// ORvar obj = {prop: [element0, element1, ...., elementN]}

 

 

var arr = [42];      // 创建一个只有唯一元素的数组:                     // the number 42.var arr = Array(42); // 创建一个没有元素的数组,                      // 但是数组的长度被设置成42.// 上面的带码与下面的代码等价var arr = [];arr.length = 42;

 

注意:如果你需要创建任意类型的单元素数组,安全的方式是使用字面值。或者在向数组添加单个元素之前先创建一个空的数组。

 

1.2 填充数据

var emp = [];emp[0] = "Casey Jones";emp[1] = "Phil Lesh";emp[2] = "August West";

 

 

var myArray = new Array("Hello", myVar, 3.14159);var myArray = ["Mango", "Apple", "Orange"]

 

 

1.3 引用数据元素

var arr = ["one", "two", "three"];arr[2];  // threearr["length"];  // 3

 

 

注意: 理解length, 在实施层面, JavaScript实际上是将元素作为标准的对象属性来存储,把数组索引作为属性名。长度属性是特殊的,它总是返回最后一个元素的索引值加1

 

var cats = [];cats[30] = [‘Dusty‘];console.log(cats.length); // 31

 

 

可以分配length属性。写一个小于数组元素数量的值会缩短数组,写0会彻底清空数组:

var cats = [‘Dusty‘, ‘Misty‘, ‘Twiggy‘];console.log(cats.length); // 3cats.length = 2;console.log(cats); // logs "Dusty,Misty" - Twiggy has been removedcats.length = 0;console.log(cats); // logs nothing; the cats array is emptycats.length = 3;console.log(cats); // [undefined, undefined, undefined]

 

 

1.4 遍历数组

最简单的

var colors = [‘red‘, ‘green‘, ‘blue‘];for (var i = 0; i < colors.length; i++) {  console.log(colors[i]);}

 

 

var divs = document.getElementsByTagName(‘div‘);for (var i = 0, div; div = divs[i]; i++) {  /* Process div in some way */}

 

 

forEach 循环

var colors = [‘red‘, ‘green‘, ‘blue‘];colors.forEach(function(color) {  console.log(color);});

 

 

var array = [‘first‘, ‘second‘, , ‘fourth‘];// returns [‘first‘, ‘second‘, ‘fourth‘];array.forEach(function(element) {  console.log(element);})if(array[2] === undefined) { console.log(‘array[2] is undefined‘); } // truevar array = [‘first‘, ‘second‘, undefined, ‘fourth‘];// returns [‘first‘, ‘second‘, undefined, ‘fourth‘];array.forEach(function(element) {  console.log(element);})

 

一旦 JavaScript 元素被保存为标准的对象属性,通过for...in 循环来迭代迭代将变得不明智,因为正常元素和所有可枚举的属性都会被列出。

 

1.5 数组方法

常用方法

concat() 连接两个数组并返回一个新的数组

var myArray = new Array("1", "2", "3");myArray = myArray.concat("a", "b", "c"); // myArray is now ["1", "2", "3", "a", "b", "c"]

 

 

join(deliminator = ",") 将数组所有元素连接成一个字符串。

var myArray = new Array("Wind", "Rain", "Fire");var list = myArray.join(" - "); // list is "Wind - Rain - Fire"

 

 

push() 在数组未尾添加一个或多个元素,并返回数组操作后的长度。

var myArray = new Array("1", "2");myArray.push("3"); // myArray is now ["1", "2", "3"]

 

 

pop() 从数组移出最后一个元素,并返回该元素。

var myArray = new Array("1", "2", "3");var last = myArray.pop(); // myArray is now ["1", "2"], last = "3"

 

 

shift() 从数组移出第一个元素,并返回该元素

var myArray = new Array ("1", "2", "3");var first = myArray.shift(); // myArray is now ["2", "3"], first is "1"

 

 

unshift() 在数组开头添加一个或多个元素,并返回数组的新长度

var myArray = new Array ("1", "2", "3");myArray.unshift("4", "5"); // myArray becomes ["4", "5", "1", "2", "3"]

 

 

slice(start_index, upto_index) 从数组提取一个片段,并作为一个新数组返回。

var myArray = new Array ("a", "b", "c", "d", "e");myArray = myArray.slice(1, 4); // starts at index 1 and extracts all elements                               // until index 3, returning [ "b", "c", "d"]

 

 

slice(index, count_to_remove, addElement1, addElement2, ...) 从数组移出一些元素,替换它们。

var myArray = new Array ("1", "2", "3", "4", "5");myArray.splice(1, 3, "a", "b", "c", "d"); // myArray is now ["1", "a", "b", "c", "d", "5"]// This code started at index one (or where the "2" was), // removed 3 elements there, and then inserted all consecutive// elements in its place.

 

 

reverse() 颠倒数组元素的顺序: 第一个变成最后一个,最后一个变成第一个。

var myArray = new Array ("1", "2", "3");myArray.reverse(); // transposes the array so that myArray = [ "3", "2", "1" ]

 

 

sort() 给数组元素排序

var myArray = new Array("Wind", "Rain", "Fire");myArray.sort(); // sorts the array so that myArray = [ "Fire", "Rain", "Wind" ]

 

 

indexOf(searchElement[, fromIndex])  在数组中搜索searchElement 并返回第一个匹配的索引。

var a = [‘a‘, ‘b‘, ‘a‘, ‘b‘, ‘a‘];console.log(a.indexOf(‘b‘)); // logs 1// Now try again, starting from after the last matchconsole.log(a.indexOf(‘b‘, 2)); // logs 3console.log(a.indexOf(‘z‘)); // logs -1, because ‘z‘ was not found

 

 

lastIndexOf(searchElement, [, fromIndex]) 和 indexOf 差不多,但这是从结尾开始,并且是反向搜索。

var a = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘a‘, ‘b‘];console.log(a.lastIndexOf(‘b‘)); // logs 5// Now try again, starting from before the last matchconsole.log(a.lastIndexOf(‘b‘, 4)); // logs 1console.log(a.lastIndexOf(‘z‘)); // logs -1

 

 

forEach(callback [, thisObject]) 在数组每个元素项上执行callback。

var a = [‘a‘, ‘b‘, ‘c‘];a.forEach(function(element) { console.log(element);} ); // logs each item in turn

 

JS - 有序数列