跳至內容

網頁程式設計/JS 陣列

來自華夏公益教科書,開放的書籍,開放的世界

陣列排序

[編輯 | 編輯原始碼]
var points = [40, 100, 1, 5, 25, 10]
points.sort((a, b) => a-b)
console.log(points)

可執行程式碼

陣列 Map Reduce

[編輯 | 編輯原始碼]
array = [{x:1},{x:2},{x:4}]
newValue = 2
result = array.map(item => item.x==newValue)
  .reduce((a,b)=>a||b)
console.log(result)

可執行程式碼

陣列 Filter

[編輯 | 編輯原始碼]
array = [{x:1},{x:2},{x:4}]
newValue = 1
newArray = array.filter(item => item.x>newValue)
console.log(newArray)

可執行程式碼

陣列 Push/Pop

[編輯 | 編輯原始碼]
var fruits = ["Orange"]
fruits.push("Kiwi")
console.log(fruits)
fruits.pop()
console.log(fruits)

可執行程式碼

陣列 Shift/Unshift

[編輯 | 編輯原始碼]
var fruits = ["Orange", "Kiwi"]
console.log(fruits)
fruits.shift()
console.log(fruits)
fruits.unshift("Apple")
console.log(fruits)

可執行程式碼

華夏公益教科書