forked from nkronlage/JavaScripture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.jsdoc
More file actions
778 lines (562 loc) · 18.7 KB
/
array.jsdoc
File metadata and controls
778 lines (562 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
Array : Object
Arrays are containers that allow access to its items
through numerical indices.
Iterable:
true
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
----
Array() : Array
Same as %%#new_Array|**new Array()**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.1
----
Array(length:Number) : Array
Same as %%#new_Array_Number|**new Array(length)**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.1.1
----
Array(item0:Object, [item1:Object, [...]]) : Array
Same as %%#new_Array_Object_Object_dotdotdot|**new Array(item0, item1, ...)**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.1.1
----
new Array() : Array
Creates an empty **Array** (**length** = 0).
Can also be constructed as **[]**.
<example>
// The following are equivalent
var x = [];
var y = Array();
var z = new Array();
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.2
----
new Array(length:Number) : Array
Creates an **Array** of the specified length where each item starts as **undefined**.
<example>
var squares = Array(5);
for (var i = 0; i < squares.length; i++) {
squares[i] = i * i;
}
console.log('squares = ' + squares);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.2.1
----
new Array(item0:Object, [item1:Object, [...]]) : Array
Creates an **Array** with the specified parameters as the 0th, 1st, ... items
of the **Array**.
Can also be constructed as **[item0, item1, ...]**.
See also %%#of|**Array.of()**%%.
<example>
// The following are equivalent
var x = ['a', 'b', 'c'];
var y = Array('a', 'b', 'c');
var z = new Array('a', 'b', 'c');
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.2.1
----
instance[index : Number] : Object
Gets and sets the item in **this** at **index**. **index** should be between **0** and
**%%#length|this.length%% - 1**. If setting to an index greater than
**length - 1**, **length** will be increased to **index + 1**.
<example>
var x = ['a', 'b', 'c'];
for (var i = 0; i < x.length; i++) {
console.log(x[i]);
}
console.log(x.length);
x[7] = 'h';
console.log(x[7]);
console.log(x.length);
</example>
----
instance.length : Number
The number of items in **this**. It is 1 greater
than the index of the last item.
<example>
console.log([].length);
console.log(['a', 'b', 'c'].length);
console.log(Array(100).length);
var x = [];
x[50] = 'foo';
console.log(x.length);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.5.2
----
prototype.concat(item0:Object, [item1:Object, [...]]) : Array
Returns a new Array composed of the items of **this** followed by
**item0, item1, ...**.
If any of the parameters are Arrays themselves, the values of that Array
will be concatenated into the new Array.
<example>
var x = [1, 2, 3];
var concatenated = x.concat(4, 5, [6, 7]);
console.log(concatenated);
console.log(concatenated.length);
console.log(x); // x is unchanged
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.4
----
prototype.copyWithin(target : Number, start : Number, [end : Number]) : Array
Copies **this[start], this[start + 1], ... this[end - 1]** to
**this[target], this[target + 1], ... this[target + start - end - 1]**.
If **end** is not specified, **this.length** will be used.
If **end** is greater than **this.length**, it is clamped to
**this.length**.
Note, no elements are copied past **this.length - 1** (ie, **copyWithin()**
will not increase the length of **this**).
Returns **this**.
<example>
var x = ['a', 'b', 'c', 'd', 'e', 'f'];
// Copy elements at end to the beginning
console.log(x.copyWithin(0, 3));
x = ['a', 'b', 'c', 'd', 'e', 'f'];
// Copy elements at the beginning to the end
console.log(x.copyWithin(3, 0));
x = ['a', 'b', 'c', 'd', 'e', 'f'];
// Copy first 3 elements to middle
console.log(x.copyWithin(2, 0, 3));
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.copywithin
----
prototype.entries() : Iterator<Array>
Returns an iterator of the index and items in **this** where the
%%Iterator#next|**values**s%% of the iterator are of the form
**[index : %%/Number|Number%%, item : %%/Object|Object%%]**.
See also %%#values|**values()**%% and %%#keys|**keys()**%%.
<example>
var x = ['a', 'b'];
// Use for (... of ...) to loop over the iterator
for (var entry of x.entries()) {
console.dir(entry);
}
// Or access the iterator manually
var entries = x.entries();
console.dir(entries.next());
console.dir(entries.next());
console.dir(entries.next());
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.entries
----
prototype.every(callback(item:Object, index:Number, array:Array):Boolean, [thisArg : Object]) : Boolean
Returns **true** if **callback** returns **true** for every item in **this**.
Otherwise returns **false**.
The **Array** passed to **callback** is the **this** of the call to **every**.
<example>
var isEven = function(x) { return (x % 2) === 0; }
console.log([2, 22, 36].every(isEven));
console.log([6, 19, 18].every(isEven));
console.log([5, 19, 17].every(isEven));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.16
----
prototype.filter(callback(item:Object, index:Number, array:Array):Boolean, [thisArg:Object]):Array
Returns a new **Array** containing only the items in **this** that **callback** returned
**true** for.
The **Array** passed to **callback** is the **this** of the call to **filter**.
<example>
var isEven = function(x) { return (x % 2) === 0; }
var numbers = [1, 2, 4, 5, 7, 9, 12];
console.log(numbers.filter(isEven));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.20
----
prototype.find(callback(item:Object, index:Number, array:Array):Boolean, [thisArg : Object]) : Object
Returns the first item in **this** where **callback** returns
**true**.
The **Array** passed to **callback** is the **this** of the call to **find**.
See also %%#findIndex|**findIndex()**%%.
<example>
var x = ['a', 'b', 'foo', 'd', 'e'];
console.log(x.find(function(item) {
return item.length > 1;
}));
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.find
----
prototype.findIndex(callback(item:Object, index:Number, array:Array):Boolean, [thisArg : Object]) : Object
Returns the index of the first item in **this** where **callback** returns
**true**.
See also %%#find|**find()**%%.
<example>
var x = ['a', 'b', 'foo', 'd', 'e'];
console.log(x.findIndex(function(item) {
return item.length > 1;
}));
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.findIndex
----
prototype.fill(value : Object, [start = 0 : Number, [end : Number]]) : Array
Fills **this[start]**, **this[start + 1]**, ... **this[end - 1]** with
**value**.
If **end** is not specified, **this.length** is used.
Returns **this**.
<example>
var x = Array(5);
console.log(x.fill('a'));
console.log(x.fill('b', 3));
console.log(x.fill('c', 0, 2));
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.fill
----
prototype.forEach(callback(item:Object, index:Number, array:Array):undefined, [thisArg:Object]):undefined
Calls **callback** for each item in **this**.
The **Array** passed to **callback** is the **this** of the call to **forEach**.
<example>
var x = ['a', 'b', 'c'];
x.forEach(function(item, index) {
console.log('item at ' + index + ' = ' + item);
});
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.18
----
prototype.indexOf(item:Object, [start = 0 : Number]):Number
Returns the first location of **item** in **this** starting the search from
**start**.
If **start**
is negative, **this.length** is added to it before starting the search.
Returns **-1** if **item** is not found.
<example>
var x = ['a', 'b', 'c', 'd', 'b', 'c', 'a'];
console.log(x.indexOf('b'));
console.log(x.indexOf('b', 2));
console.log(x.indexOf('b', -4));
console.log(x.indexOf('e', 2));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.14
----
prototype.join([separator=',':String]):String
Returns a String created by joining the **toString()** of each item of
**this** separated by **separator**.
<example>
var x = ['a', 'b', 'c'];
console.log(x.join());
console.log(x.join(' -- '));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.5
----
prototype.keys() : Iterator<Number>
Returns an iterator of the indicies in **this**.
See also %%#entries|**entries()**%% and %%#keys|**keys()**%%.
<example>
var x = ['a', 'b'];
// Use for (... of ...) to loop over the iterator
for (var key of x.keys()) {
console.dir(key);
}
// Or access the iterator manually
var keys = x.keys();
console.dir(keys.next());
console.dir(keys.next());
console.dir(keys.next());
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.keys
----
prototype.lastIndexOf(item:Object, [start:Number]):Number
Returns the location of **item** in **this** starting the search from
**start** by searching backwards through the array.
If **startingIndex** is not specified, the search starts from the
end of the array.
If **start**
is negative, **this.length** is added to it before starting the search.
Returns **-1** if **item** is not found.
<example>
var x = ['a', 'b', 'c', 'd', 'b', 'c', 'a'];
console.log(x.lastIndexOf('b'));
console.log(x.lastIndexOf('b', 3));
console.log(x.lastIndexOf('b', -4));
console.log(x.lastIndexOf('e', 2));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.15
----
prototype.map(callback(item:Object, index:Number, array:Array):Object, [thisArg:Object]):Array
Returns a new **Array** with where each item is the result of calling
**callback** on each item in **this**.
The **Array** passed to **callback** is the **this** of the call to **map**.
<example>
var numbers = [1, 2, 3, 4];
var squares = numbers.map(function(x) { return x * x; });
console.log(squares);
console.log(numbers);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.19
----
prototype.pop():Object
Removes the last item from **this** and returns the removed item.
Returns **undefined** if **this** is empty.
Use with %%#push|**push**%% to treat an **Array** as a stack.
<example>
var x = ['a', 'b', 'c'];
console.log(x.pop());
console.log(x);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.6
----
prototype.push(item0:Object, [item1:Object, [...]]):Number
Appends the specified items to the end of **this** and returns the new
value of **this.length**. Use with %%#pop|**pop**%% to treat an **Array** as a
stack.
<example>
var x = ['a', 'b', 'c'];
console.log(x.push('d', 'e'));
console.log(x);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.7
----
prototype.reduce(callback(previous:Object, current:Object,index:Number,array:Array):Object, [initialValue:Object]):Object
Calls **callback** for each item in **this** in ascending order (**0** to **length-1**).
It passes the return value of **callback** for the **i-1**th item as the
**previous** parameter for the **i**th item.
Returns the result from the last call to **callback**.
If **initialValue** is not specified, **callback** will first
be called on **this[1]** with **previous** set to **this[0]**.
The **Array** passed to **callback** is the **this** of the call to **reduce**.
<example>
var join = function(prev, cur) { return prev + cur; }
console.log(['b', 'c'].reduce(join, 'a'));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.21
----
prototype.reduceRight(callback(previous:Object, current:Object,index:Number,array:Array):Object, [initialValue:Object]):Object
Calls **callback** for each item in **this** in descending order (**length-1** to **0**).
It passes the return value of **callback** for the **i+1**th item as the
**previous** parameter for the **i**th item.
Returns the result from the last call to **callback**.
If **initialValue** is not specified, **callback** will first
be called on **this[this.length - 2]** with **previous** set to **this[this.length - 1]**.
The **Array** passed to **callback** is the **this** of the call to **reduceRight**.
<example>
var join = function(prev, cur) { return prev + cur; }
console.log(['b', 'c'].reduceRight(join, 'a'));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.22
----
prototype.reverse() : Array
Reverses the order of the items in **this** and returns **this**.
<example>
var x = ['a', 'b', 'c'];
console.log(x.reverse());
console.log(x);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.8
----
prototype.shift():Object
Removes the first item of **this** and shifts the
remaining items down by **1**. Returns the removed item.
<example>
var x = ['a', 'b', 'c'];
console.log(x.shift());
console.log(x);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.9
----
prototype.slice(start:Number, [end:Number]):Array
Returns a new **Array** which is composed of the items **this[start],
this[start + 1], ..., this[end - 1]**. Note that
**item[end]** is not included. If **start** or **end**
is negative, the value is added to **this.length** before performing
the slice. If **end** is not specified, **this.length** is used.
<example>
var x = ['a', 'b', 'c', 'd', 'e'];
console.log(x.slice(2));
console.log(x.slice(2, 3));
console.log(x.slice(1, -2));
console.log(x.slice(-4, -1));
console.log(x);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.10
----
prototype.some(callback(item : Object, index : Number, array:Array):Boolean, [thisArg:Object]) : Boolean
Returns **true** if **callback** returns **true** for at least one item in
**this**. Otherwise returns **false**.
The **Array** passed to **callback** is the **this** of the call to **some**.
<example>
var isEven = function(x) { return (x % 2) === 0; }
console.log([2, 22, 36].some(isEven));
console.log([6, 19, 18].some(isEven));
console.log([5, 19, 17].some(isEven));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.17
----
prototype.sort([comparisonFunction(x:Object, y:Object):Number]):Array
Sort the items of **this** using **comparisonFunction** to determine
the sort order and returns **this**.
The Number returned by **comparisonFunction** should be **0** if **x** and
**y** are equal, negative if **x** is less than **y**, or positive if **x**
is greater than **y**. If **comparisonFunction** is not specified, the **toString()**
of the items will be sorted in alphanumeric order.
Returns **this**.
<example>
var x = [52, 7, 14, 22, 9];
// Sort alphanumerically
console.log(x.sort());
console.log(x);
// Sort numerically
var numericComparison = function(x, y) {
return x - y;
};
console.log(x.sort(numericComparison));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.11
----
prototype.splice(start:Number, count:Number, [item0:Object, [item1:Object, [...]]]):Array
Removes **count** items from **this** starting at index
**start**. If the optional **items** are specified, they are
inserted into **this** at **start**. Returns a new **Array** containing
the removed items.
<example>
var x = ['a', 'b', 'c', 'd', 'e'];
console.log(x.splice(2, 1));
console.log(x);
console.log(x.splice(1, 2, 'X', 'Y', 'Z'));
console.log(x);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.12
----
prototype.unshift(item0:Object, [item1:Object, [...]]):Number
Inserts the specified items at the start of **this**.
Returns the new value of **this.length**.
<example>
var x = ['c', 'd', 'e'];
console.log(x.unshift('a', 'b'));
console.log(x);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.13
----
prototype.values() : Iterator
Returns an iterator of the items in **this**.
The **values** function is also returned for
**this[%%/Symbol#iterator|Symbol.iterator%%]** so you can
iterate over **this** directly to get the values.
See also %%#entries|**entries()**%% and %%#keys|**keys()**%%.
<example>
var x = ['a', 'b'];
// values is returned by x[Symbol.iterator] so you can just
// use x directly in the for (... of ...) loop
for (var value of x) {
console.dir(value);
}
// You can iterate over values explicitly
for (var value of x.values()) {
console.dir(value);
}
// Or access the iterator manually
var values = x.values();
console.dir(values.next());
console.dir(values.next());
console.dir(values.next());
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.values
----
from(arrayLike : Object, [mapFunction(item : Object, index : Number) : Object, [thisArg : Object]]) : Boolean
Returns a new **Array** with the elements of **arrayLike**.
**from()** will return
a new **Array** of length **arrayLike.length** where the elements are
**arrayLike[0]**, **arrayLike[1]**, ..., **arrayLike[arrayLike.length - 1]**.
<example>
// From a string
console.log(Array.from('foo'));
// With a map function
var mapFunction = function(value) {
return value.toUpperCase();
};
console.log(Array.from('foo', mapFunction));
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.from
----
from(iterator : Iterator, [mapFunction(item : Object, index : Number) : Object, [thisArg : Object]]) : Boolean
Returns a new **Array** with the elements of **iterator** containing the
items returned by the iterator.
<example>
var generator = function*() {
yield* ['a', 'b', 'c'];
};
console.log(Array.from(generator()));
// With a map function
var mapFunction = function(value) {
return value.toUpperCase();
};
console.log(Array.from(generator(), mapFunction));
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.from
----
isArray(value : Object) : Boolean
Returns **true** if **value** is an **Array**.
<example>
console.log(Array.isArray([]));
console.log(Array.isArray({}));
console.log(Array.isArray(53));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.3.2
----
of(item0 : Object, [item1 : Object, [...]]) : Array
Creates an **Array** with the specified parameters as the
0th, 1st, ... items of the **Array**.
Can also be constructed as **[item0, item1, ...]**.
Note that **of(x)** will always create an array of length **1**
containing **x**,
while %%#new_Array_Number|**new Array(x)**%% which will create
an array of length **x** if **x** is an integer.
<example>
// The following are equivalent
var w = ['a', 'b', 'c'];
var x = Array('a', 'b', 'c');
var y = new Array('a', 'b', 'c');
var z = Array.of('a', 'b', 'c');
</example>
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.of