这篇整理一些常用的阵列方法
unshift 会添加一个或多个元素至阵列的开头,并回传阵列的新长度
语法:
Array.unshift(要新增的资料)
范例:
var a=[1,2,3]
console.log(a.unshift(4,5)); // 5
console.log(a); // [4,5,1,2,3]
reverse 会将原阵列反转,并且回传原阵列
语法:
Array.reverse()
范例:
var a=['one','two','three'];
var reversed=a.reverse();
console.log(a); // ['three','two','one']
console.log(reversed); // ['three','two','one']
reverse 会将原阵列反转,并且回传原阵列的位置
所以修改阵列 a 也会修改到阵列 reversed。
var a=['one','two','three'];
var reversed=a.reverse();
console.log(a); // ['three','two','one']
console.log(reversed); // ['three','two','one']
reversed.push('four');
console.log(a); // ['three','two','one','four']
console.log(reversed); //['three','two','one','four']
可以删除既有元素来改变原阵列。
或者,删除既有元素後加入新元素来改变阵列。
语法:
splice 包含三个参数
Array.splice(start,deleteCount,item)
范例:
var a=[1,2,3,4,5,6,7,8];
a.splice(5,1); //
console.log(a); // [1,2,3,4,5,6,7,8]
设定第三个参数就可以添加或取代元素
var a=[1,2,3,4,5,6,7,8];
a.splice(5,1,100);
console.log(a); // [1,2,3,4,5,100,7,8] ( 6 被移除,100 被加到第 5 个位置)
var b=[1,2,3,4,5,6,7,8];
b.splice(5,3,100,300,500);
console.log(b); // [1,2,3,4,5,100,300,500] (6,7,8被移除,100,300,500被加进index为5,6,7位置)
var c=[1,2,3,4,5,6,7,8];
c.splice(5,0,300);
console.log(c); // [1,2,3,4,5,300,6,7,8] (没有元素被移除,100被加进index为5的位置)
<<: [Day 13] 第一主餐 pt.6-叮咚,有您的包裹
除了监听事件外,jQuery也提供了定义好的动态效果函式,让开发者直接使用,并透过传入相关参数,去自...
今天将会介绍简易的todo list 功能 新增事项(push将输入值放入阵列) Enter加入资料...
终於来到尾声了,今天我们将对 Next.js 来做一番介绍,介绍完後,我们会跟这二十九天来的战友 G...
再来要介绍框线属性 border-style(框线样式) 框线的样式有很多种,例如:none(无,预...
再来就是实际建立透过 select 选择的脚位,并建立相关 Firmata 功能。 建立 PWM 控...