{"id":194273,"date":"2020-06-24T08:21:19","date_gmt":"2020-06-24T00:21:19","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=194273"},"modified":"2020-06-22T09:21:47","modified_gmt":"2020-06-22T01:21:47","slug":"es6-array-introduction","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/es6-array-introduction.html","title":{"rendered":"ES6 \u6570\u7ec4\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nES6 \u4e3b\u8981\u662f\u4e3a\u4e86\u89e3\u51b3 ES5 \u7684\u5148\u5929\u4e0d\u8db3\uff0c\u6bd4\u5982 JavaScript \u91cc\u5e76\u6ca1\u6709\u7c7b\u7684\u6982\u5ff5\uff0c\u4f46\u662f\u76ee\u524d\u6d4f\u89c8\u5668\u7684 JavaScript \u662f ES5 \u7248\u672c\uff0c\u5927\u591a\u6570\u9ad8\u7248\u672c\u7684\u6d4f\u89c8\u5668\u4e5f\u652f\u6301 ES6\uff0c\u4e0d\u8fc7\u53ea\u5b9e\u73b0\u4e86 ES6 \u7684\u90e8\u5206\u7279\u6027\u548c\u529f\u80fd\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u6570\u7ec4\u521b\u5efa<\/strong><\/div>\n
Array.of()<\/strong><\/span><\/div>\n

\u5c06\u53c2\u6570\u4e2d\u6240\u6709\u503c\u4f5c\u4e3a\u5143\u7d20\u5f62\u6210\u6570\u7ec4\u3002<\/p>\n

console.log(Array.of(1, 2, 3, 4)); \/\/ [1, 2, 3, 4]\r\n \r\n\/\/ \u53c2\u6570\u503c\u53ef\u4e3a\u4e0d\u540c\u7c7b\u578b\r\nconsole.log(Array.of(1, '2', true)); \/\/ [1, '2', true]\r\n \r\n\/\/ \u53c2\u6570\u4e3a\u7a7a\u65f6\u8fd4\u56de\u7a7a\u6570\u7ec4\r\nconsole.log(Array.of()); \/\/ []<\/pre>\n
Array.from()<\/strong><\/span><\/div>\n

\u5c06\u7c7b\u6570\u7ec4\u5bf9\u8c61\u6216\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u5316\u4e3a\u6570\u7ec4\u3002<\/p>\n

\/\/ \u53c2\u6570\u4e3a\u6570\u7ec4,\u8fd4\u56de\u4e0e\u539f\u6570\u7ec4\u4e00\u6837\u7684\u6570\u7ec4\r\nconsole.log(Array.from([1, 2])); \/\/ [1, 2]\r\n \r\n\/\/ \u53c2\u6570\u542b\u7a7a\u4f4d\r\nconsole.log(Array.from([1, , 3])); \/\/ [1, undefined, 3]<\/pre>\n

\u53c2\u6570<\/strong><\/p>\n

Array.from(arrayLike[, mapFn[, thisArg]])<\/pre>\n

\u8fd4\u56de\u503c\u4e3a\u8f6c\u6362\u540e\u7684\u6570\u7ec4\u3002<\/p>\n

arrayLike<\/strong><\/p>\n

\u60f3\u8981\u8f6c\u6362\u7684\u7c7b\u6570\u7ec4\u5bf9\u8c61\u6216\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n

console.log(Array.from([1, 2, 3])); \/\/ [1, 2, 3]<\/pre>\n

mapFn<\/strong><\/p>\n

\u53ef\u9009\uff0cmap \u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\uff0c\u653e\u5165\u6570\u7ec4\u7684\u662f\u5904\u7406\u540e\u7684\u5143\u7d20\u3002<\/p>\n

console.log(Array.from([1, 2, 3], (n) => n * 2)); \/\/ [2, 4, 6]<\/pre>\n

thisArg<\/strong><\/p>\n

\u53ef\u9009\uff0c\u7528\u4e8e\u6307\u5b9a map \u51fd\u6570\u6267\u884c\u65f6\u7684 this \u5bf9\u8c61\u3002<\/p>\n

let map = {\r\n    do: function(n) {\r\n        return n * 2;\r\n    }\r\n}\r\nlet arrayLike = [1, 2, 3];\r\nconsole.log(Array.from(arrayLike, function (n){\r\n    return this.do(n);\r\n}, map)); \/\/ [2, 4, 6]<\/pre>\n
\u7c7b\u6570\u7ec4\u5bf9\u8c61<\/strong><\/span><\/div>\n

\u4e00\u4e2a\u7c7b\u6570\u7ec4\u5bf9\u8c61\u5fc5\u987b\u542b\u6709 length \u5c5e\u6027\uff0c\u4e14\u5143\u7d20\u5c5e\u6027\u540d\u5fc5\u987b\u662f\u6570\u503c\u6216\u8005\u53ef\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u5b57\u7b26\u3002<\/p>\n

let arr = Array.from({\r\n  0: '1',\r\n  1: '2',\r\n  2: 3,\r\n  length: 3\r\n});\r\nconsole.log(); \/\/ ['1', '2', 3]\r\n \r\n\/\/ \u6ca1\u6709 length \u5c5e\u6027,\u5219\u8fd4\u56de\u7a7a\u6570\u7ec4\r\nlet array = Array.from({\r\n  0: '1',\r\n  1: '2',\r\n  2: 3,\r\n});\r\nconsole.log(array); \/\/ []\r\n \r\n\/\/ \u5143\u7d20\u5c5e\u6027\u540d\u4e0d\u4e3a\u6570\u503c\u4e14\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u503c\uff0c\u8fd4\u56de\u957f\u5ea6\u4e3a length \u5143\u7d20\u503c\u4e3a undefined \u7684\u6570\u7ec4  \r\nlet array1 = Array.from({\r\n  a: 1,\r\n  b: 2,\r\n  length: 2\r\n});\r\nconsole.log(array1); \/\/ [undefined, undefined]<\/pre>\n
\u8f6c\u6362\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/strong><\/span><\/div>\n

\u8f6c\u6362 map<\/strong><\/p>\n

let map = new Map();\r\nmap.set('key0', 'value0');\r\nmap.set('key1', 'value1');\r\nconsole.log(Array.from(map)); \/\/ [['key0', 'value0'],['key1',\r\n\/\/ 'value1']]<\/pre>\n

\u8f6c\u6362 set<\/strong><\/p>\n

let arr = [1, 2, 3];\r\nlet set = new Set(arr);\r\nconsole.log(Array.from(set)); \/\/ [1, 2, 3]<\/pre>\n

\u8f6c\u6362\u5b57\u7b26\u4e32<\/strong><\/p>\n

let str = 'abc';\r\nconsole.log(Array.from(str)); \/\/ [\"a\", \"b\", \"c\"]<\/pre>\n
\u6269\u5c55\u7684\u65b9\u6cd5<\/strong><\/div>\n
\u67e5\u627e<\/strong><\/span><\/div>\n

find()<\/strong><\/p>\n

\u67e5\u627e\u6570\u7ec4\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20,\u82e5\u6709\u591a\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n

let arr = Array.of(1, 2, 3, 4);\r\nconsole.log(arr.find(item => item > 2)); \/\/ 3\r\n \r\n\/\/ \u6570\u7ec4\u7a7a\u4f4d\u5904\u7406\u4e3a undefined\r\nconsole.log([, 1].find(n => true)); \/\/ undefined<\/pre>\n

findIndex()<\/strong><\/p>\n

\u67e5\u627e\u6570\u7ec4\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u7d22\u5f15\uff0c\u82e5\u6709\u591a\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\u7d22\u5f15\u3002<\/p>\n

let arr = Array.of(1, 2, 1, 3);\r\n\/\/ \u53c2\u65701\uff1a\u56de\u8c03\u51fd\u6570\r\n\/\/ \u53c2\u65702(\u53ef\u9009)\uff1a\u6307\u5b9a\u56de\u8c03\u51fd\u6570\u4e2d\u7684 this \u503c\r\nconsole.log(arr.findIndex(item => item = 1)); \/\/ 0\r\n \r\n\/\/ \u6570\u7ec4\u7a7a\u4f4d\u5904\u7406\u4e3a undefined\r\nconsole.log([, 1].findIndex(n => true)); \/\/0<\/pre>\n
\u586b\u5145<\/strong><\/span><\/div>\n

fill()<\/strong><\/p>\n

\u5c06\u4e00\u5b9a\u8303\u56f4\u7d22\u5f15\u7684\u6570\u7ec4\u5143\u7d20\u5185\u5bb9\u586b\u5145\u4e3a\u5355\u4e2a\u6307\u5b9a\u7684\u503c\u3002<\/p>\n

let arr = Array.of(1, 2, 3, 4);\r\n\/\/ \u53c2\u65701\uff1a\u7528\u6765\u586b\u5145\u7684\u503c\r\n\/\/ \u53c2\u65702\uff1a\u88ab\u586b\u5145\u7684\u8d77\u59cb\u7d22\u5f15\r\n\/\/ \u53c2\u65703(\u53ef\u9009)\uff1a\u88ab\u586b\u5145\u7684\u7ed3\u675f\u7d22\u5f15\uff0c\u9ed8\u8ba4\u4e3a\u6570\u7ec4\u672b\u5c3e\r\nconsole.log(arr.fill(0,1,2)); \/\/ [1, 0, 3, 4]<\/pre>\n

copyWithin()<\/strong><\/p>\n

\u5c06\u4e00\u5b9a\u8303\u56f4\u7d22\u5f15\u7684\u6570\u7ec4\u5143\u7d20\u4fee\u6539\u4e3a\u6b64\u6570\u7ec4\u53e6\u4e00\u6307\u5b9a\u8303\u56f4\u7d22\u5f15\u7684\u5143\u7d20\u3002<\/p>\n

\/\/ \u53c2\u65701\uff1a\u88ab\u4fee\u6539\u7684\u8d77\u59cb\u7d22\u5f15\r\n\/\/ \u53c2\u65702\uff1a\u88ab\u7528\u6765\u8986\u76d6\u7684\u6570\u636e\u7684\u8d77\u59cb\u7d22\u5f15\r\n\/\/ \u53c2\u65703(\u53ef\u9009)\uff1a\u88ab\u7528\u6765\u8986\u76d6\u7684\u6570\u636e\u7684\u7ed3\u675f\u7d22\u5f15\uff0c\u9ed8\u8ba4\u4e3a\u6570\u7ec4\u672b\u5c3e\r\nconsole.log([1, 2, 3, 4].copyWithin(0,2,4)); \/\/ [3, 4, 3, 4]\r\n \r\n\/\/ \u53c2\u65701\u4e3a\u8d1f\u6570\u8868\u793a\u5012\u6570\r\nconsole.log([1, 2, 3, 4].copyWithin(-2, 0)); \/\/ [1, 2, 1, 2]\r\n \r\nconsole.log([1, 2, ,4].copyWithin(0, 2, 4)); \/\/ [, 4, , 4]<\/pre>\n
\u904d\u5386<\/strong><\/span><\/div>\n

entries()<\/strong><\/p>\n

\u904d\u5386\u952e\u503c\u5bf9\u3002<\/p>\n

for(let [key, value] of ['a', 'b'].entries()){\r\n    console.log(key, value);\r\n}\r\n\/\/ 0 \"a\"\r\n\/\/ 1 \"b\"\r\n \r\n\/\/ \u4e0d\u4f7f\u7528 for... of \u5faa\u73af\r\nlet entries = ['a', 'b'].entries();\r\nconsole.log(entries.next().value); \/\/ [0, \"a\"]\r\nconsole.log(entries.next().value); \/\/ [1, \"b\"]\r\n \r\n\/\/ \u6570\u7ec4\u542b\u7a7a\u4f4d\r\nconsole.log([...[,'a'].entries()]); \/\/ [[0, undefined], [1, \"a\"]]<\/pre>\n

keys()<\/strong><\/p>\n

\u904d\u5386\u952e\u540d\u3002<\/p>\n

for(let key of ['a', 'b'].keys()){\r\n    console.log(key);\r\n}\r\n\/\/ 0\r\n\/\/ 1\r\n \r\n\/\/ \u6570\u7ec4\u542b\u7a7a\u4f4d\r\nconsole.log([...[,'a'].keys()]); \/\/ [0, 1]<\/pre>\n

values()<\/strong><\/p>\n

\u904d\u5386\u952e\u503c\u3002<\/p>\n

for(let value of ['a', 'b'].values()){\r\n    console.log(value);\r\n}\r\n\/\/ \"a\"\r\n\/\/ \"b\"\r\n \r\n\/\/ \u6570\u7ec4\u542b\u7a7a\u4f4d\r\nconsole.log([...[,'a'].values()]); \/\/ [undefined, \"a\"]<\/pre>\n
\u5305\u542b<\/strong><\/span><\/div>\n

includes()<\/strong><\/p>\n

\u6570\u7ec4\u662f\u5426\u5305\u542b\u6307\u5b9a\u503c\u3002<\/p>\n

\u6ce8\u610f\uff1a\u4e0e Set \u548c Map \u7684 has \u65b9\u6cd5\u533a\u5206\uff1bSet \u7684 has \u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u503c\uff1bMap \u7684 has \u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u952e\u540d\u3002<\/p>\n

\/\/ \u53c2\u65701\uff1a\u5305\u542b\u7684\u6307\u5b9a\u503c\r\n[1, 2, 3].includes(1);    \/\/ true\r\n \r\n\/\/ \u53c2\u65702\uff1a\u53ef\u9009\uff0c\u641c\u7d22\u7684\u8d77\u59cb\u7d22\u5f15\uff0c\u9ed8\u8ba4\u4e3a0\r\n[1, 2, 3].includes(1, 2); \/\/ false\r\n \r\n\/\/ NaN \u7684\u5305\u542b\u5224\u65ad\r\n[1, NaN, 3].includes(NaN); \/\/ true<\/pre>\n
\u5d4c\u5957\u6570\u7ec4\u8f6c\u4e00\u7ef4\u6570\u7ec4<\/strong><\/span><\/div>\n

flat()<\/strong><\/p>\n

console.log([1 ,[2, 3]].flat()); \/\/ [1, 2, 3]\r\n \r\n\/\/ \u6307\u5b9a\u8f6c\u6362\u7684\u5d4c\u5957\u5c42\u6570\r\nconsole.log([1, [2, [3, [4, 5]]]].flat(2)); \/\/ [1, 2, 3, [4, 5]]\r\n \r\n\/\/ \u4e0d\u7ba1\u5d4c\u5957\u591a\u5c11\u5c42\r\nconsole.log([1, [2, [3, [4, 5]]]].flat(Infinity)); \/\/ [1, 2, 3, 4, 5]\r\n \r\n\/\/ \u81ea\u52a8\u8df3\u8fc7\u7a7a\u4f4d\r\nconsole.log([1, [2, , 3]].flat());

\/\/ [1, 2, 3]<\/p><\/pre>\n

flatMap()<\/strong>
\n\u5148\u5bf9\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u4e86\u7684\u5904\u7406\uff0c\u518d\u5bf9\u6570\u7ec4\u6267\u884c flat() \u65b9\u6cd5\u3002<\/p>\n

\/\/ \u53c2\u65701\uff1a\u904d\u5386\u51fd\u6570\uff0c\u8be5\u904d\u5386\u51fd\u6570\u53ef\u63a5\u53d73\u4e2a\u53c2\u6570\uff1a\u5f53\u524d\u5143\u7d20\u3001\u5f53\u524d\u5143\u7d20\u7d22\u5f15\u3001\u539f\u6570\u7ec4\r\n\/\/ \u53c2\u65702\uff1a\u6307\u5b9a\u904d\u5386\u51fd\u6570\u4e2d this \u7684\u6307\u5411\r\nconsole.log([1, 2, 3].flatMap(n => [n * 2])); \/\/ [2, 4, 6]<\/pre>\n
\u6570\u7ec4\u7f13\u51b2\u533a<\/strong><\/div>\n

\u6570\u7ec4\u7f13\u51b2\u533a\u662f\u5185\u5b58\u4e2d\u7684\u4e00\u6bb5\u5730\u5740\u3002<\/p>\n

\u5b9a\u578b\u6570\u7ec4\u7684\u57fa\u7840\u3002<\/p>\n

\u5b9e\u9645\u5b57\u8282\u6570\u5728\u521b\u5efa\u65f6\u786e\u5b9a\uff0c\u4e4b\u540e\u53ea\u53ef\u4fee\u6539\u5176\u4e2d\u7684\u6570\u636e\uff0c\u4e0d\u53ef\u4fee\u6539\u5927\u5c0f\u3002<\/p>\n

\u521b\u5efa\u6570\u7ec4\u7f13\u51b2\u533a<\/strong><\/span><\/div>\n

\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521b\u5efa:<\/p>\n

let buffer = new ArrayBuffer(10);\r\nconsole.log(buffer.byteLength); \/\/ 10\r\n\u5206\u5272\u5df2\u6709\u6570\u7ec4\u7f13\u51b2\u533a\r\nlet buffer = new ArrayBuffer(10);\r\nlet buffer1 = buffer.slice(1, 3);\r\nconsole.log(buffer1.byteLength); \/\/ 2<\/pre>\n
\u89c6\u56fe<\/strong><\/span><\/div>\n

\u89c6\u56fe\u662f\u7528\u6765\u64cd\u4f5c\u5185\u5b58\u7684\u63a5\u53e3\u3002<\/p>\n

\u89c6\u56fe\u53ef\u4ee5\u64cd\u4f5c\u6570\u7ec4\u7f13\u51b2\u533a\u6216\u7f13\u51b2\u533a\u5b57\u8282\u7684\u5b50\u96c6,\u5e76\u6309\u7167\u5176\u4e2d\u4e00\u79cd\u6570\u503c\u6570\u636e\u7c7b\u578b\u6765\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u3002<\/p>\n

DataView \u7c7b\u578b\u662f\u4e00\u79cd\u901a\u7528\u7684\u6570\u7ec4\u7f13\u51b2\u533a\u89c6\u56fe,\u5176\u652f\u6301\u6240\u67098\u79cd\u6570\u503c\u578b\u6570\u636e\u7c7b\u578b\u3002<\/p>\n

\u521b\u5efa:<\/p>\n

\/\/ \u9ed8\u8ba4 DataView \u53ef\u64cd\u4f5c\u6570\u7ec4\u7f13\u51b2\u533a\u5168\u90e8\u5185\u5bb9\r\nlet buffer = new ArrayBuffer(10);\r\n    dataView = new DataView(buffer); \r\ndataView.setInt8(0,1);\r\nconsole.log(dataView.getInt8(0)); \/\/ 1\r\n \r\n\/\/ \u901a\u8fc7\u8bbe\u5b9a\u504f\u79fb\u91cf(\u53c2\u65702)\u4e0e\u957f\u5ea6(\u53c2\u65703)\u6307\u5b9a DataView \u53ef\u64cd\u4f5c\u7684\u5b57\u8282\u8303\u56f4\r\nlet buffer1 = new ArrayBuffer(10);\r\n    dataView1 = new DataView(buffer1, 0, 3);\r\ndataView1.setInt8(5,1); \/\/ RangeError<\/pre>\n
\u5b9a\u578b\u6570\u7ec4<\/strong><\/div>\n

\u6570\u7ec4\u7f13\u51b2\u533a\u7684\u7279\u5b9a\u7c7b\u578b\u7684\u89c6\u56fe\u3002<\/p>\n

\u53ef\u4ee5\u5f3a\u5236\u4f7f\u7528\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u901a\u7528\u7684 DataView \u5bf9\u8c61\u6765\u64cd\u4f5c\u6570\u7ec4\u7f13\u51b2\u533a\u3002<\/p>\n

\u521b\u5efa<\/strong><\/span><\/div>\n

\u901a\u8fc7\u6570\u7ec4\u7f13\u51b2\u533a\u751f\u6210<\/p>\n

let buffer = new ArrayBuffer(10),\r\n    view = new Int8Array(buffer);\r\nconsole.log(view.byteLength); \/\/ 10<\/pre>\n

\u901a\u8fc7\u6784\u9020\u51fd\u6570<\/p>\n

let view = new Int32Array(10);\r\nconsole.log(view.byteLength); \/\/ 40\r\nconsole.log(view.length);     \/\/ 10\r\n \r\n\/\/ \u4e0d\u4f20\u53c2\u5219\u9ed8\u8ba4\u957f\u5ea6\u4e3a0\r\n\/\/ \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6570\u7ec4\u7f13\u51b2\u533a\u5206\u914d\u4e0d\u5230\u7a7a\u95f4\uff0c\u521b\u5efa\u7684\u5b9a\u578b\u6570\u7ec4\u4e0d\u80fd\u7528\u6765\u4fdd\u5b58\u6570\u636e\r\nlet view1 = new Int32Array();\r\nconsole.log(view1.byteLength); \/\/ 0\r\nconsole.log(view1.length);     \/\/ 0\r\n \r\n\/\/ \u53ef\u63a5\u53d7\u53c2\u6570\u5305\u62ec\u5b9a\u578b\u6570\u7ec4\u3001\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3001\u6570\u7ec4\u3001\u7c7b\u6570\u7ec4\u5bf9\u8c61\r\nlet arr = Array.from({\r\n  0: '1',\r\n  1: '2',\r\n  2: 3,\r\n  length: 3\r\n});\r\nlet view2 = new Int16Array([1, 2]),\r\n    view3 = new Int32Array(view2),\r\n    view4 = new Int16Array(new Set([1, 2, 3])),\r\n    view5 = new Int16Array([1, 2, 3]),\r\n    view6 = new Int16Array(arr);\r\nconsole.log(view2 .buffer === view3.buffer); \/\/ false\r\nconsole.log(view4.byteLength); \/\/ 6\r\nconsole.log(view5.byteLength); \/\/ 6\r\nconsole.log(view6.byteLength); \/\/ 6<\/pre>\n
\u6ce8\u610f\u8981\u70b9<\/strong><\/span><\/div>\n

length \u5c5e\u6027\u4e0d\u53ef\u5199\uff0c\u5982\u679c\u5c1d\u8bd5\u4fee\u6539\u8fd9\u4e2a\u503c\uff0c\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\u4f1a\u76f4\u63a5\u5ffd\u7565\u8be5\u64cd\u4f5c\uff0c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n

let view = new Int16Array([1, 2]);\r\nview.length = 3;\r\nconsole.log(view.length); \/\/ 2<\/pre>\n

\u5b9a\u578b\u6570\u7ec4\u53ef\u4f7f\u7528 entries()\u3001keys()\u3001values()\u8fdb\u884c\u8fed\u4ee3\u3002<\/p>\n

let view = new Int16Array([1, 2]);\r\nfor(let [k, v] of view.entries()){\r\n    console.log(k, v);\r\n}\r\n\/\/ 0 1\r\n\/\/ 1 2<\/pre>\n

find() \u7b49\u65b9\u6cd5\u4e5f\u53ef\u7528\u4e8e\u5b9a\u578b\u6570\u7ec4\uff0c\u4f46\u662f\u5b9a\u578b\u6570\u7ec4\u4e2d\u7684\u65b9\u6cd5\u4f1a\u989d\u5916\u68c0\u67e5\u6570\u503c\u7c7b\u578b\u662f\u5426\u5b89\u5168,\u4e5f\u4f1a\u901a\u8fc7 Symbol.species \u786e\u8ba4\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u662f\u5b9a\u578b\u6570\u7ec4\u800c\u975e\u666e\u901a\u6570\u7ec4\u3002concat() \u65b9\u6cd5\u7531\u4e8e\u4e24\u4e2a\u5b9a\u578b\u6570\u7ec4\u5408\u5e76\u7ed3\u679c\u4e0d\u786e\u5b9a\uff0c\u6545\u4e0d\u80fd\u7528\u4e8e\u5b9a\u578b\u6570\u7ec4\uff1b\u53e6\u5916\uff0c\u7531\u4e8e\u5b9a\u578b\u6570\u7ec4\u7684\u5c3a\u5bf8\u4e0d\u53ef\u66f4\u6539,\u53ef\u4ee5\u6539\u53d8\u6570\u7ec4\u7684\u5c3a\u5bf8\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982 splice() \uff0c\u4e0d\u9002\u7528\u4e8e\u5b9a\u578b\u6570\u7ec4\u3002<\/p>\n

let view = new Int16Array([1, 2]);\r\nview.find((n) > 1); \/\/ 2<\/pre>\n

\u6240\u6709\u5b9a\u578b\u6570\u7ec4\u90fd\u542b\u6709\u9759\u6001 of() \u65b9\u6cd5\u548c from() \u65b9\u6cd5,\u8fd0\u884c\u6548\u679c\u5206\u522b\u4e0e Array.of() \u65b9\u6cd5\u548c Array.from() \u65b9\u6cd5\u76f8\u4f3c,\u533a\u522b\u662f\u5b9a\u578b\u6570\u7ec4\u7684\u65b9\u6cd5\u8fd4\u56de\u5b9a\u578b\u6570\u7ec4,\u800c\u666e\u901a\u6570\u7ec4\u7684\u65b9\u6cd5\u8fd4\u56de\u666e\u901a\u6570\u7ec4\u3002<\/p>\n

let view = Int16Array.of(1, 2);\r\nconsole.log(view instanceof Int16Array); \/\/ true<\/pre>\n

\u5b9a\u578b\u6570\u7ec4\u4e0d\u662f\u666e\u901a\u6570\u7ec4\uff0c\u4e0d\u7ee7\u627f\u81ea Array \u3002<\/p>\n

let view = new Int16Array([1, 2]);\r\nconsole.log(Array.isArray(view)); \/\/ false<\/pre>\n

\u5b9a\u578b\u6570\u7ec4\u4e2d\u589e\u52a0\u4e86 set() \u4e0e subarray() \u65b9\u6cd5\u3002 set() \u65b9\u6cd5\u7528\u4e8e\u5c06\u5176\u4ed6\u6570\u7ec4\u590d\u5236\u5230\u5df2\u6709\u5b9a\u578b\u6570\u7ec4, subarray() \u7528\u4e8e\u63d0\u53d6\u5df2\u6709\u5b9a\u578b\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u5f62\u6210\u65b0\u7684\u5b9a\u578b\u6570\u7ec4\u3002<\/p>\n

\/\/ set \u65b9\u6cd5\r\n\/\/ \u53c2\u65701\uff1a\u4e00\u4e2a\u5b9a\u578b\u6570\u7ec4\u6216\u666e\u901a\u6570\u7ec4\r\n\/\/ \u53c2\u65702\uff1a\u53ef\u9009\uff0c\u504f\u79fb\u91cf\uff0c\u5f00\u59cb\u63d2\u5165\u6570\u636e\u7684\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u4e3a0\r\nlet view= new Int16Array(4);\r\nview.set([1, 2]);\r\nview.set([3, 4], 2);\r\nconsole.log(view); \/\/ [1, 2, 3, 4]\r\n \r\n\/\/ subarray \u65b9\u6cd5\r\n\/\/ \u53c2\u65701\uff1a\u53ef\u9009\uff0c\u5f00\u59cb\u4f4d\u7f6e\r\n\/\/ \u53c2\u65702\uff1a\u53ef\u9009\uff0c\u7ed3\u675f\u4f4d\u7f6e(\u4e0d\u5305\u542b\u7ed3\u675f\u4f4d\u7f6e)\r\nlet view= new Int16Array([1, 2, 3, 4]), \r\n    subview1 = view.subarray(), \r\n    subview2 = view.subarray(1), \r\n    subview3 = view.subarray(1, 3);\r\nconsole.log(subview1); \/\/ [1, 2, 3, 4]\r\nconsole.log(subview2); \/\/ [2, 3, 4]\r\nconsole.log(subview3); \/\/ [2, 3]<\/pre>\n
\u6269\u5c55\u8fd0\u7b97\u7b26<\/strong><\/div>\n
\u590d\u5236\u6570\u7ec4<\/strong><\/span><\/div>\n
let arr = [1, 2],\r\n    arr1 = [...arr];\r\nconsole.log(arr1); \/\/ [1, 2]\r\n \r\n\/\/ \u6570\u7ec4\u542b\u7a7a\u4f4d\r\nlet arr2 = [1, , 3],\r\n    arr3 = [...arr2];\r\nconsole.log(arr3); [1, undefined, 3]<\/pre>\n

\u5408\u5e76\u6570\u7ec4<\/p>\n

console.log([...[1, 2],...[3, 4]]); \/\/ [1, 2, 3, 4]<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5c06\u53c2\u6570\u4e2d\u6240\u6709\u503c\u4f5c\u4e3a\u5143\u7d20\u5f62\u6210\u6570\u7ec4\u3002 console.log(Array.of(1, 2, 3, 4)); \/\/ […]<\/p>\n","protected":false},"author":1470,"featured_media":194278,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-194273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=194273"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194273\/revisions"}],"predecessor-version":[{"id":194280,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194273\/revisions\/194280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/194278"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=194273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=194273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=194273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}