当前位置 : 主页 > 大数据 > 区块链 >

Array Usage collection

来源:互联网 收集:自由互联 发布时间:2021-06-22
I'm so used to using array prototype methods because it makes my code simpler and more efficient. I could focus more on logic design or business service. What so many kinds of methods! Sometimes, we may be confusedabout when and how to use,

I'm so used to using array prototype methods because it makes my code simpler and more efficient. I could focus more on logic design or business service. What so many kinds of methods! Sometimes, we may be confused about when and how to use, or when the source array are secretly forced to be changed...

So I made a short summary here just for reference:

go through, find, check, create to be as another type one, create to be an array,

change source array and create a new array.

#Gthrough: in order to do other actions when going through an array

Methods
Comments
Updates in
forEach   ES5 reduce   ES5 reduceRight the same as "reserve().reduce()" ES5

#Find: find in an array

Methods
Comments
Updates in
array.find(callback) return an item from an array ES6 array.findIndex(callback) return index number ES6 array.indexOf(element[, fromIndex]) return index number ES5 array.lastIndexOf(element[, fromIndex]) return index number

ES5







#Check: check with a callback in an array

Methods
Comments
Updates in
array.every(callback) return true only when all elements in the array pass the callback ES5 array.includes(element[, fromIndex]) return true only when an array includes a certain element ES6 array.some(callback) return true when at least one element in the array passes the callback ES5




#Create to be as another type one

Methods
Comments
Updates in
array.entries()

returns a new Array Iterator object that contains the key/value pairs for each index in the array.

ES6 array.keys() returns a new Array Iterator object that contains the keys for each index in the array. ES6 array.values() returns a new Array Iterator object that contains the values for each index in the array. ES6 array.join() joins all elements of an array (or an array-like object) into a string and returns this string. ES5 array.toString() same as array.join(',') before ES5 array.toLocalString() use toLocalString method of object, number, Date and then join all elements with ',' before ES5

#Create to be an array from other array-like object or iterable object

Methods
Comments
Updates in
Array.from(array-like)

creates a new, shallow-copied Array instance from an array-like or iterable object.

ES6

Array.of(element0[...,[elementN]])

creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.

ES6

#Change source array

Methods
Comments
Updates in
array.splice(start[, deleteCount[, item1[, ...[,itemN]]]]) remove existing elements and/or add new elements before ES5

array.copyWithin(target[,start[,end]])

e.g. var a=[1,2,3]; a.copyWithin(1,2,3); //expected a: [1,3, 3]

ES6

array.fill(value[,start[,end]])

e.g. var a=[1,2,3]; a.fill(1,2,3); //expected a: [1,2,1]
ES6 array.pop()
before ES5 array.push(element1[, ...[,elementN]]) Array.prototype.push.apply(array1, array2) equals array1 = array1.concat(array2); before ES5 array.sort(callback)
before ES5

array.reverse()

e.g. var a=["a", "c", "d"]; a.reverse(); //expected a: ["d", "c", "a"]

before ES5

array.shift() e.g. var a=["a", "c", "d"]; a.shift(); //expected a: ["c", "d"] before ES5 array.unshift()

e.g. var a=["a", "c", "d"]; a.unshift("e","f"); //expected a: ["e", "f", "a", "c", "d"]

before ES5














#Create a new array and don't affect source array

Methods
Comments
Updates in
array1.concat(array2)
before ES5 array.filter(callback)
ES5 array.map(callback)
ES5 array.flatMap(callback)
experimental API array.flat(depth)
experimental API array.slice(begin[, end]) e.g. var a=[1,2,3]; var b = a.slice(1); //expected b: [2,3] before ES5
网友评论