最适合网络开发者的网站

JS 教程

JS 首页 JS 简介 JS 去哪儿 JS 输出 JS 语句 JS 语法 JS 评论 JS 变量 JS 让 JS 常量 JS 运算符 JS 算术 JS 作业 JS 数据类型 JS 函数 JS 对象 JS 活动 JS 字符串 JS 字符串方法 JS 字符串搜索 JS 字符串模板 JS 数字 JS 数字方法 JS 数组 JS 数组方法 JS 数组排序 JS 数组迭代 JS 数组常量 JS 日期 JS 日期格式 JS 获取日期方法 JS 日期设置方法 JS 数学 JS 随机 JS 布尔值 JS 比较 JS 条件 JS 开关 JS 循环 JS 循环 JS 循环 JS 循环 JS 中断 JS 可迭代对象 JS 集 JS 地图 JS Typeof JS 类型转换 JS 按位 JS 正则表达式 JS 错误 JS 作用域 JS 提升 JS 严格模式 JS this 关键字 JS 箭头函数 JS 类 JS JSON JS 调试 JS 风格指南 JS 最佳实践 JS 错误 JS 性能 JS 保留字

JS 版本

JS 版本 JS 2009(ES5) JS 2015(ES6) 小学 2016 小学 2017 小学 2018 JS IE/Edge JS 历史

JS 对象

对象定义 对象属性 对象方法 对象显示 对象访问器 对象构造函数 对象原型 对象可迭代对象 对象集 对象映射 对象引用

JS 函数

函数定义 函数参数 函数调用 函数调用 功能应用 函数绑定 函数闭包

JS 类

课程简介 类继承 静态类

JS 异步

JS 回调 JS 异步 JS 承诺 JS 异步/等待

JS HTML DOM

DOM 简介 DOM 方法 DOM 文档 DOM 元素 DOM HTML DOM 表单 DOM CSS DOM 动画 DOM 事件 DOM 事件监听器 DOM 导航 DOM 节点 DOM 集合 DOM 节点列表

JS 浏览器 BOM

JS 窗口 JS 屏幕 JS 位置 JS 历史 JS 导航器 JS 弹出警告 JS 时机 JS Cookies

JS Web API

Web API 简介 Web 表单 API 网络历史记录 API Web 存储 API Web Worker API Web 获取 API 网络地理定位 API

JS AJAX

AJAX 简介 AJAX XMLHttp AJAX 请求 AJAX 响应 AJAX XML 文件 AJAX PHP AJAX ASP AJAX 数据库 AJAX 应用程序 AJAX 示例

JS JSON

JSON 简介 JSON 语法 JSON 与 XML JSON 数据类型 JSON 解析 JSON 字符串化 JSON 对象 JSON 数组 JSON 服务器 JSON 格式的 PHP JSON 的 HTML JSON JSONP

JS 与 jQuery

jQuery 选择器 jQuery HTML jQuery CSS jQuery DOM

JS 图形

JS 图形 JS 画布 JS Plotly JS Chart.js JS 谷歌图表 JS D3.js

JS 示例

JS 示例 JS HTML DOM JS HTML 输入 JS HTML 对象 JS HTML 事件 JS 浏览器 JS 编辑器 JS 练习 JS 测验 JS 证书

JS 参考

JavaScript 对象 HTML DOM 对象

JavaScript。W3Schools 英文版。初学者课程

尿素

JavaScript Array Methods


Converting Arrays to Strings

The JavaScript method toString() converts an array to a string of (comma separated) array values.

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
document.getElementById("demo").innerHTML = fruits.toString();

Result:

Banana,Orange,Apple,Mango
亲自尝试 »

join() method also joins all array elements into a string.

It behaves just like toString(), but in addition you can specify the separator:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
document.getElementById("demo").innerHTML = fruits.join(" * ");

Result:

Banana * Orange * Apple * Mango
亲自尝试 »

Popping and Pushing

When you work with arrays, it is easy to remove elements and add new elements.

This is what popping and pushing is:

Popping items out of an array, or pushing items into an array.


JavaScript Array pop()

pop() method removes the last element from an array:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits.pop();
亲自尝试 »

pop() method returns the value that was "popped out":

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
let fruit = fruits.pop();
亲自尝试 »

JavaScript Array push()

push() method adds a new element to an array (at the end):

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits.push("Kiwi");
亲自尝试 »

push() method returns the new array length:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
let length = fruits.push("Kiwi");
亲自尝试 »

Shifting Elements

Shifting is equivalent to popping, but working on the first element instead of the last.


JavaScript Array shift()

shift() method removes the first array element and "shifts" all other elements to a lower index.

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits.shift();
亲自尝试 »

shift() method returns the value that was "shifted out":

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
let fruit = fruits.shift();
亲自尝试 »

JavaScript Array unshift()

unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits.unshift("Lemon");
亲自尝试 »

unshift() method returns the new array length.

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits.unshift("Lemon");
亲自尝试 »

Changing Elements

Array elements are accessed using their 索引号:

Array indexes start with 0:

[0] is the first array element
[1] is the second
[2] is the third ...

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits[0] = "Kiwi";
亲自尝试 »

JavaScript Array length

length property provides an easy way to append a new element to an array:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits[fruits.length] = "Kiwi";
亲自尝试 »

JavaScript Array delete()

Warning!

Array elements can be deleted using the JavaScript operator delete.

Using delete leaves undefined holes in the array.

Use pop() or shift() instead.

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
delete fruits[0];
亲自尝试 »

Merging (Concatenating) Arrays

concat() method creates a new array by merging (concatenating) existing arrays:

Example (Merging Two Arrays)

const myGirls = ["Cecilie", "Lone"];
const myBoys = ["Emil", "Tobias", "Linus"];

const myChildren = myGirls.concat(myBoys);
亲自尝试 »

concat() method does not change the existing arrays. It always returns a new array.

concat() method can take any number of array arguments:

Example (Merging Three Arrays)

const arr1 = ["Cecilie", "Lone"];
const arr2 = ["Emil", "Tobias", "Linus"];
const arr3 = ["Robin", "Morgan"];
const myChildren = arr1.concat(arr2, arr3);
亲自尝试 »

concat() method can also take strings as arguments:

Example (Merging an Array with Values)

const arr1 = ["Emil", "Tobias", "Linus"];
const myChildren = arr1.concat("Peter"); 
亲自尝试 »

Splicing and Slicing Arrays

splice() method adds new items to an array.

slice() method slices out a piece of an array.


JavaScript Array splice()

splice() method can be used to add new items to an array:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits.splice(2, 0, "Lemon", "Kiwi");
亲自尝试 »

The first parameter (2) defines the position where new elements should be added (spliced in).

The second parameter (0) defines how many elements should be removed.

The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.

splice() method returns an array with the deleted items:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits.splice(2, 2, "Lemon", "Kiwi");
亲自尝试 »

Using splice() to Remove Elements

With clever parameter setting, you can use splice() to remove elements without leaving "holes" in the array:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
fruits.splice(0, 1);
亲自尝试 »

The first parameter (0) defines the position where new elements should be added (spliced in).

The second parameter (1) defines how many elements should be removed.

The rest of the parameters are omitted. No new elements will be added.


JavaScript Array slice()

slice() method slices out a piece of an array into a new array.

This example slices out a part of an array starting from array element 1 ("Orange"):

例子

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1);
亲自尝试 »

Note

slice() method creates a new array.

slice() method does not remove any elements from the source array.

This example slices out a part of an array starting from array element 3 ("Apple"):

例子

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(3);
亲自尝试 »

slice() method can take two arguments like slice(1, 3).

The method then selects elements from the start argument, and up to (but not including) the end argument.

例子

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);
亲自尝试 »

If the end argument is omitted, like in the first examples, the slice() method slices out the rest of the array.

例子

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(2);
亲自尝试 »

Automatic toString()

JavaScript automatically converts an array to a comma separated string when a primitive value is expected.

This is always the case when you try to output an array.

These two examples will produce the same result:

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
document.getElementById("demo").innerHTML = fruits.toString();
亲自尝试 »

例子

const 水果 = ["香蕉", "橙子", "苹果", "芒果"];
document.getElementById("demo").innerHTML = fruits;
亲自尝试 »

Note

All JavaScript objects have a toString() method.


Finding Max and Min Values in an Array

There are no built-in functions for finding the highest or lowest value in a JavaScript array.

You will learn how you solve this problem in the next chapter of this tutorial.


Sorting Arrays

Sorting arrays are covered in the next chapter of this tutorial.

完整数组参考

有关完整的 Array 参考,请访问:

完整的 JavaScript 数组参考.

该参考包含所有 Array 属性和方法的描述和示例。

通过练习测试自己

锻炼:

Use the correct Array method to remove the last item of the fruits 大批。

const fruits = ["Banana", "Orange", "Apple"];
;