array.push(element1[, ...[, elementn]])
const array =
array.push(1)
console.log(array) //=> [1]
array.push(2, 3)
console.log(array) //=> [1, 2, 3]
console.log(array.length) //=> 3
array.unshift(element1[, ...[, elementn]])
const array = [ 4, 5 ]
array.unshift(3)
console.log(array) //=> [3, 4, 5]
array.unshift(1, 2)
console.log(array) //=> [1, 2, 3, 4, 5]
array.splice(start, deletecount, element1[, ...[, elementn]])
`第二個引數是刪除若干個元素,設定0為不刪除任何元素`
const array = [ 1, 2, 6, 7 ]
array.splice(2, 0, 3)
console.log(array) //=> [1, 2, 3, 6, 7]
array.splice(3, 0, 4, 5)
console.log(array) //=> [1, 2, 3, 4, 5, 6, 7]
array.splice(start, deletecount, element1[, ...[, elementn]])
const array = [1, 2, 3, 10, 4, 5]
array.splice(3, 1)
console.log(array) //=> [1, 2, 3, 4, 5]
1.
const array = [ 1, 2, 3, 4, 5 ]
array[0] = 10
console.log(array) //=> [10, 2, 3, 4, 5]
2.const array = [ 1, 2, 3, 4, 5 ]
array.splice(1,1,6)
console.log(array) //=> [ 1, 6, 3, 4, 5 ]
//splice(inde,howmany,item)引數 描述
//index 必需。規定從何處新增/刪除元素。該引數是開始插入和(或)刪除的陣列元素的下標,必須是數字。
//howmany 必需。規定應該刪除多少元素。必須是數字,但可以是 "0"。如果未規定此引數,則刪除從 index 開始到原陣列結尾的所有元素。
//item,要新增到陣列的新元素
array.filter(fn)
const array = [ 1, 2, 3, 4, 5, 6, 7, 8 ]
const evennumbers = array.filter(function(x) )
console.log(evennumbers) //=> [2, 4, 6, 8]
const array = [ 1, 2, 3, 4, 5 ]
const addedarray = array.map(function(x) )
console.log(addedarray) //=> [3,4,5,6,7]
const array = [1,2,3,4]
1.array.reduce((left,right) => )
`針對每次所需要用到的聚合情況做些封裝`
const arrayutils = )
} function multi(array) )
}} `或者更為抽象的方式,涉及一些函式式程式設計的概念`
function reducefn(fn)
}
isinclude (aa, bb) );
});},
isinclude (aa, bb) );
});},
陣列複製的幾種方法
一 for迴圈 二 使用system.arraycopy方法 system.arraycopy src,srcpos,dest,destpos,length 可以選定複製原陣列的部分內容,但新陣列需要先宣告並初始化 另外這種方法可以用來實現原陣列的擴容,即將原陣列的內容拷進去,但是比較麻煩 三 ar...
陣列排序的幾種方法
演算法步驟 比較相鄰的元素。如果第乙個比第二個大,就交換他們兩個。對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最後一對。這步做完後,最後的元素會是最大的數。針對所有的元素重複以上的步驟,除了最後乙個。持續每次對越來越少的元素重複上面的步驟,直到沒有任何一對數字需要比較。氣泡排序 functio...
陣列遍歷的幾種方法
陣列遍歷 let arr 1,2,3,4,5,6,7,8 let res let obj for let i 0 i arr.length i for let i 0 i obj.length i for let item of arr for let item of obj 方法對陣列的每個元素執...