Javascript Array

Arrays

Arrays are zero-indexed, ordered lists of values. They are a handy way to
store a set of related items of the same type (such as strings), though in
reality, an array can include multiple types of items, including other
arrays.

To create an array, either use the object constructor or the literal
declaration, by assigning the variable a list of values after the
declaration.





1

2

3

4

5

// A simple array with constructor.

var myArray1 = new Array( "hello", "world" );

// Literal declaration, the preferred way.

var myArray2 = [ "hello", "world" ];

The literal declaration is generally preferred. See the Google
Coding Guidelines
 for more information.

If the values are unknown, it is also possible to declare an empty array, and
add elements either through functions or through accessing by index:





1

2

3

4

5

6

7

8

9

10

11

12

// Creating empty arrays and adding values

var myArray = [];

// Adds "hello" on index 0

myArray.push( "hello" );

// Adds "world" on index 1

myArray.push( "world" );

// Adds "!" on index 2

myArray[ 2 ] = "!";

.push() is a function that adds an element on the end of
the array and expands the array respectively. You also can directly add items by
index. Missing indices will be filled with undefined.





1

2

3

4

5

6

7

8

9

// Leaving indices

var myArray = [];

myArray[ 0 ] = "hello";

myArray[ 1 ] = "world";

myArray[ 3 ] = "!";

console.log( myArray ); // [ "hello", "world", undefined, "!" ];

If the size of the array is unknown, .push() is far
more safe. You can both access and assign values to array items with the
index.





1

2

3

4

5

// Accessing array items by index

var myArray = [ "hello", "world", "!" ];

console.log( myArray[ 2 ] ); // "!"

Array Methods and Properties


.length

The .length property is used to determine the amount
of items in an array.





1

2

3

4

5

// Length of an array

var myArray = [ "hello", "world", "!" ];

console.log( myArray.length ); // 3

You will need the .length property for looping through
an array:





1

2

3

4

5

6

7

8

9

// For loops and arrays - a classic

var myArray = [ "hello", "world", "!" ];

for ( var i = 0; i < myArray.length; i = i + 1 ) {

console.log( myArray[ i ] );

}

.concat()

Concatenate two or more arrays with .concat():





1

2

3

var myArray = [ 2, 3, 4 ];

var myOtherArray = [ 5, 6, 7 ];

var wholeArray = myArray.concat( myOtherArray ); // [ 2, 3, 4, 5, 6, 7 ]

.join()

.join() creates a string representation of an array by
joining all of its elements using a separator string. If no separator is
supplied (i.e., .join() is called without arguments) the
array will be joined using a comma.





1

2

3

4

5

6

7

8

9

10

11

12

13

// Joining elements

var myArray = [ "hello", "world", "!" ];

// The default separator is a comma.

console.log( myArray.join() ); // "hello,world,!"

// Any string can be used as separator...

console.log( myArray.join( " " ) ); // "hello world !";

console.log( myArray.join( "!!" ) ); // "hello!!world!!!";

// ...including an empty one.

console.log( myArray.join( "" ) ); // "helloworld!"

.pop()

.pop() removes the last element of an array. It is the
opposite method of .push():





1

2

3

4

5

6

7

8

// Pushing and popping

var myArray = [];

myArray.push( 0 ); // [ 0 ]

myArray.push( 2 ); // [ 0 , 2 ]

myArray.push( 7 ); // [ 0 , 2 , 7 ]

myArray.pop(); // [ 0 , 2 ]

link.reverse()

As the name suggests, the elements of the array are in reverse order after
calling this method:





1

2

var myArray = [ "world" , "hello" ];

myArray.reverse(); // [ "hello", "world" ]

.shift()

Removes the first element of an array.
With .push() and .shift(), you can
recreate the method of a queue:





1

2

3

4

5

6

7

8

// Queue with shift() and push()

var myArray = [];

myArray.push( 0 ); // [ 0 ]

myArray.push( 2 ); // [ 0 , 2 ]

myArray.push( 7 ); // [ 0 , 2 , 7 ]

myArray.shift(); // [ 2 , 7 ]

.slice()

Extracts a part of the array and returns that part in a new array. This
method takes one parameter, which is the starting index:





1

2

3

4

5

6

7

// Slicing

var myArray = [ 1, 2, 3, 4, 5, 6, 7, 8 ];

var newArray = myArray.slice( 3 );

console.log( myArray ); // [ 1, 2, 3, 4, 5, 6, 7, 8 ]

console.log( newArray ); // [ 4, 5, 6, 7, 8 ]

.splice()

Removes a certain amount of elements and adds new ones at the given index. It
takes at least three parameters:





1

myArray.splice( index, length, values, ... );

  • Index – The starting index.

  • Length – The number of elements to remove.

  • Values – The values to be inserted at the index
    position.

For example:





1

2

3

4

var myArray = [ 0, 7, 8, 5 ];

myArray.splice( 1, 2, 1, 2, 3, 4 );

console.log( myArray ); // [ 0, 1, 2, 3, 4, 5 ]

.sort()

Sorts an array. It takes one parameter, which is a comparing function. If
this function is not given, the array is sorted ascending:





1

2

3

4

5

// Sorting without comparing function.

var myArray = [ 3, 4, 6, 1 ];

myArray.sort(); // 1, 3, 4, 6





1

2

3

4

5

6

7

8

9

// Sorting with comparing function.

function descending( a, b ) {

return b - a;

}

var myArray = [ 3, 4, 6, 1 ];

myArray.sort( descending ); // [ 6, 4, 3, 1 ]

The return value of descending (for this example) is important. If the return
value is less than zero, the index of a is
before b, and if it is greater than zero it‘s vice-versa. If
the return value is zero, the elements‘ index is equal.

.unshift()

Inserts an element at the first position of the array:





1

2

3

4

5

var myArray = [];

myArray.unshift( 0 ); // [ 0 ]

myArray.unshift( 2 ); // [ 2 , 0 ]

myArray.unshift( 7 ); // [ 7 , 2 , 0 ]

.forEach()

In modern browsers it is possible to traverse through arrays with
.forEach() method, where you pass a function that is
called for each element in the array.

The function takes up to three arguments:

  • Element – The element itself.

  • Index – The index of this element in the array.

  • Array – The array itself.

All of these are optional, but you will need at least
the Element parameter in most cases.





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

// Native .forEach()

function printElement( elem ) {

console.log( elem );

}

function printElementAndIndex( elem, index ) {

console.log( "Index " + index + ": " + elem );

}

function negateElement( elem, index, array ) {

array[ index ] = -elem;

}

myArray = [ 1, 2, 3, 4, 5 ];

// Prints all elements to the console

myArray.forEach( printElement );

// Prints "Index 0: 1", "Index 1: 2", "Index 2: 3", ...

myArray.forEach( printElementAndIndex );

// myArray is now [ -1, -2, -3, -4, -5 ]

myArray.forEach( negateElement );

Javascript Array,布布扣,bubuko.com

时间: 2024-10-20 06:55:36

Javascript Array的相关文章

javascript Array学习

首先感谢Dash 我再也不用到处乱找文档了 再次感谢日食记 让我的看到了世界的美好 好的 我们进入正题 注解 我所有的学习心得都建立在ECMAscript5之后 IE9之前的浏览器概不负责 javascript Array是一个好玩的对象 如何检测她呢 首先instanceof是个不错的方法 if (value instanceof Array) { } 不过根据javascript高级程序设计说 这样做 如果一个人重新构造了Array函数 你完了 so 这样 if(Arrays.isArray

JavaScript - Array对象的使用 及 数组排序 sort

<html> <head> <head> <body> <script language="javascript"> // Array对象 // 第一种构造方法 var arr = new Array(); alert(arr.length); arr[0] = 520 ; arr[1] = "wjp" ; alert(arr.length); // 第二种构造方法 // Array(4) ; // 第三种

javascript Array.push pop unshit shit

HTML代码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>Document</title> 7 <style type="text/css"> 8 *{font-family:Consolas;font-style: italic} 9 .sh

JavaScript Array对象介绍

Array 数组 1. 介绍 数组是值的有序集合.每个值叫做一个元素,而每个元素在数组中有一个位置,以数字表示,称为索引.JavaScript数组是无类型:数组元素可以是任意类型,并且同一个数组中的不同元素也可能有不同的类型. --<JavaScript权威指南(第六版)> 2. 定义 var names = new Array("张三", "李四", "王五"); //或者 var names = ["张三",

JavaScript Array(数组)对象

一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, element1, ..., elementn); 参数 参数 size 是期望的数组元素个数.返回的数组,length 字段将被设为 size 的值. 参数 element ..., elementn 是参数列表.当使用这些参数来调用构造函数 Array() 时,新创建的数组的元素就会被初始化为这些值.它

JavaScript Array 对象参考手册

JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", "BMW"]; 第一个数组元素的索引值为 0,第二个索引值为 1,以此类推. 更多有关JavaScript Array参考手册请参考 JavaScript Array 对象手册. Array 对象属性 方法 描述 concat() 连接两个或更多的数组,并返回结果. every() 检测数值

Javascript Array Distinct

javascript 没有原生的Distinct功能 . (至少现在还没有)但我们可以通过简单的script 自己实现 . Distinct就是把数组中重复出现2次或以上的值给删除掉,确保数组内每个值都是唯一的 . 我相信大家开始的时候都会和我用同一个方法来处理.那就是开一个新的数组(空),然后 for loop 旧的数组 ,然后复制进去新的数组里面,每次复制进去的时候先检查一篇新数组内是否有了这个值,有了就跳过,没有才加进去 . 代码 :  var old_array = [1, 2, 3,

Javascript Array 非常用方法解析

1. map var ary = Array(3); ary[0] = 2 ary.map(function(elem) { return '1'; }); 结果是["1", undefined * 2], 因为map 只能被初始化过的数组成员调用 2. reduce [].reduce(Math.pow): //typeError, 空数组上调用reduce [3,2,1].reduce(function(x, y) { console.log(x, y); return Math.

JavaScript Array --&gt;map()、filter()、reduce()、forEach()函数的使用

题目: 1.得到 3000 到 3500 之内工资的人. 2.增加一个年龄的字段,并且计算其年龄. 3.打印出每个人的所在城市 4.计算所有人的工资的总和. 测试数据: function getData() { var arr = [{ id: 1, name: 'ohzri', birth: '1999.09.09', city: '湖北', salary: 9379 }, { id: 2, name: 'rqgfd', birth: '1999.10.28', city: '湖北', sal