Array Usage collection

本文总结了JavaScript中数组的各种原型方法,包括遍历、查找、校验等操作,并介绍了如何创建新数组或将现有数组转换为其他类型。此外还讨论了修改源数组的方法及如何创建新数组而不影响原数组。

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









//************mouse buttons+sensor+wheel**********************// 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x02, // Usage (Mouse) 0xA1, 0x01, // Collection (Application) 0x85, 0x01, 0x09, 0x01, 0xA1, 0x00, // Usage (Pointer) 0x05, 0x09, // Collection (Physical) 0x19, 0x01, 0x29, 0x05, // Usage Page (Buttons) 0x15, 0x00, // Usage Minimum (1 buttons) 0x25, 0x01, // Usage Maximum (6 buttons) 0x75, 0x01, // Logic Minimum (0) 0x95, 0x05, // Logic Maximum (1) 0x81, 0x02, 0x75, 0x01, // Report Size (1bit) 0x95, 0x03, // Report Count (6) 0x81, 0x03, // Input (Data, Variable, Absolute) - Button Byte 0x05, 0x01, 0x09, 0x38, // Usage Page (Reserved) 0x15, 0x81, // Report Size (1bit) 0x25, 0x7F, // Report Count (2) 0x75, 0x08, // Input (Constant, Variable, Absolute) - Button Padding 0x95, 0x01, 0x81, 0x06, // Usage (X, Y) 0x09, 0x30, // Report Size (8bits) 0x09, 0x31, // Report Count (2) 0x16, 0x00, 0x80, // Logical Minimum (-127) 0x26, 0xFF, 0x7F, // Logical Maximum (+127) 0x75, 0x10, // Input (Data, Variable, Absolute) - X, Y Bytes 0x95, 0x02, 0x81, 0x06, // Usage Page (Generic Desktop) 0x05, 0x0C, // Usage (Scroll Wheel) 0x0A, 0x38, 0x02, // Logical Minimum (-127) 0x15, 0x81, // Logical Maximum (+127) 0x25, 0x7F, // Report Size (8) 0x75, 0x08, // Report Count (1) 0x95, 0x01, // Input (Data, Variable, Relative) - Z Bytes 0x81, 0x06, // Usage (X) 0x09, 0x00, // Usage (Y) 0x15, 0x00, // Logical Minimum (-32767) 0x25, 0x01, // Logical Maximum (+32767) 0x75, 0x08, // Report Size (16) 0x95, 0x01, // Report Count (2) 0x81, 0x06, // Input (Data, Variable, Relative) - X, Y Words 0xC0, // End Collection (Physical) 0x09, 0x05, // Usage (Vendor Defined) 0x15, 0x00, // Logical Minimum (0) 0x26, 0xFF, 0x00, // Logical Maximum (255) 0x75, 0x08, // Report Size (8 bit) 0x95, 0x40, // Report Count (2) 0xB1, 0x02, // Feature (Data, Variable, Absolute) 0xC0, // End Collection (Application) 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x06, // Usage (Keyboard) 0xA1, 0x01, // Collection (Application) 0x85, 0x02, 0x05, 0x07, // Usage Page (Key Codes) 0x19, 0xe0, // Usage Minimum (224) 0x29, 0xe7, // Usage Maximum (231) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x01, // Logical Maximum (1) 0x75, 0x01, // Report Size (1) 0x95, 0x08, // Report Count (8) 0x81, 0x02, // Input (Data, Variable, Absolute) 0x05,0x07, //; Usage Page(Key Codes) 6 bytes for keycode 0x19,0x00, //; Usage Min(00) 0x2A,0xFF,0x00, //; Usage Max(0x00FF) 0x15,0x00, //; Logical Min(0) 0x26,0xFF,0x00, //; Logical Max(0x00FF) 0x75,0x08, //; Report size(8) 0x95,0x06, //; Report count(6) 0x81,0x00, //; Output(Data, Array, Absolute) 0xC0, //;End Collection //************Consumer**********************// 0x05, 0x0C, // Usage Page (Consumer) 0x09, 0x01, // Usage (Consumer) 0xA1, 0x01, // Collection (Application) 0x85, 0x03, // Report ID of Multimedia keys - Record ID, 1-byte (0x02) 0x19, 0x00, // Usage Min (0x0000) 0x2A, 0x3C, 0x02, // Usage Max (0x023C) 0x15, 0x00, // Logical Min (0x0000) 0x26, 0x3C, 0x02, // Logical Max (0x023C) 0x95, 0x01, // Report Count (1) 0x75, 0x10, // Report Size (16) 0x81, 0x00, // Input (Data, Array, Absolute) - Media Key, 1-word 0x95, 0x05, // Report Count (13) 0x75, 0x08, // Report Size (8) 0x81, 0x01, // Input (Constant, Array, Absolute) - Packet Padding, 5-byte 0xC0, // End Collection 0x05, 0x01, // Usage Page (Generic desktop) 0x09, 0x80, // Usage (System Control) 0xA1, 0x01, // Collection (Application) 0x85, 0x04, // Report ID of Power keys - Record ID, 1-byte (0x03) 0x19, 0x81, // Usage Min (0x81) 0x29, 0x83, // Usage Max (0x83) 0x15, 0x00, // Logical Min (0) 0x25, 0x01, // Logical Max (1) 0x75, 0x01, // Report Size (1) 0x95, 0x03, // Report Count (3) 0x81, 0x02, // Input (Data, Variable, Absolute) - Power Keys, 3-bits 0x95, 0x05, // Report Count (5) 0x81, 0x01, // Input (Constant, Array, Absolute) - Byte Padding, 5-bits 0x75, 0x07, // Report Size (8) 0x95, 0x08, // Report Count (14) 0x81, 0x01, // Input (Constant, Array, Absolute) - Packet Padding, 6-bytes 0xC0, // End_Collection 0x06, 0xA0, 0xFF, // 0x09, 0x01, // 0xA1, 0x01, // 0x85, 0x05, // 0x09, 0x01, // 0x15, 0x00, // 0x26, 0xFF, 0x00, // 0x75, 0x08, // 0x95, 0x08, // 0x81, 0x02, // 0xC0, // 0x06, 0xFF, 0xFF, // 0x09, 0x01, // 0xA1, 0x01, // 0x85, 0x06, // 0x09, 0x01, // 0x15, 0x00, // 0x26, 0xFF, 0x00, // 0x75, 0x08, // 0x95, 0x08, // 0x81, 0x02, // 0xc0,
07-12
uint8_t hid_report_desc[]={ 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x05, // Usage (Game Pad) 0xa1, 0x01, // Collection (Application) 0x15, 0x00, // Logical minimum (0) 0x25, 0x01, // Logical maximum (1) 0x35, 0x00, // Physical minimum (0) 0x45, 0x01, // Physical maximum (1) 0x75, 0x01, // Report Size (1) 0x95, 0x0e, // Report Count (14) 0x05, 0x09, // Usage Page (Button) 0x19, 0x01, // Usage Minimum (Button 1) 0x29, 0x0e, // Usage Maximum (Button 14) 0x81, 0x02, // Input (Data,Value,Absolute,Bit Field) 0x95, 0x02, // Report Count (2) 0x81, 0x01, // Input (Constant,Array,Absolute,Bit Field) 0x05, 0x01, // Usage Page (Generic Desktop) 0x25, 0x07, // Logical maximum (7) 0x46, 0x3b, 0x01, // Physical maximum (315) 0x75, 0x04, // Report Size (4) 0x95, 0x01, // Report Count (1) 0x65, 0x14, // Unit (English Rotation: Angular Position [degrees]) 0x09, 0x39, // Usage (Hat switch) 0x81, 0x42, // Input (Data,Value,Absolute,Bit Field) 0x65, 0x00, // Unit (None) 0x95, 0x01, // Report Count (1) 0x81, 0x01, // Input (Constant,Array,Absolute,Bit Field) 0x26, 0xff, 0x00, // Logical maximum (255) 0x46, 0xff, 0x00, // Physical maximum (-1) 0x09, 0x30, // Usage (X) 0x09, 0x31, // Usage (Y) 0x09, 0x32, // Usage (Z) 0x09, 0x35, // Usage (Rz) 0x75, 0x08, // Report Size (8) 0x95, 0x04, // Report Count (4) 0x81, 0x02, // Input (Data,Value,Absolute,Bit Field) 0x75, 0x08, // Report Size (8) 0x95, 0x01, // Report Count (1) 0x81, 0x01, // Input (Constant,Array,Absolute,Bit Field) // New Axes Definitions 0x09, 0x33, // USAGE (Rx Axis) 0x09, 0x34, // USAGE (Ry Axis) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8 bits) 0x95, 0x02, // REPORT_COUNT (2 new axes) 0x81, 0x02, // INPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) // Example of extending the report descriptor with a custom 38-byte field. // 0x05, 0xFF, // Usage Page (Vendor Defined) 0x09, 0x20, // Usage (Custom Data Field ID 1) // 0xA1, 0x01, // Collection (Application) 0x75, 0x08, // Report Size (8 bits per logical value) 0x95, 0x0e, // Report Count (14 values total) 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) 0xc0, // End Collection }; 解析这段报告描述符
05-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值