Array Usage collection

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
reduceRightthe same as "reserve().reduce()"ES5

#Find: find in an array

Methods
Comments
Updates in
array.find(callback)return an item from an arrayES6
array.findIndex(callback)return index numberES6
array.indexOf(element[, fromIndex])return index numberES5
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 callbackES5
array.includes(element[, fromIndex])return true only when an array includes a certain elementES6
array.some(callback)return true when at least one element in the array passes the callbackES5




#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 elementsbefore 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









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值