You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`arr.reverse()` Reverses the order of the elements of an array in place — the first becomes the last, and the last becomes the first.
399
399
-`arr.shift()` Removes the first element from an array and returns that element.
400
400
-`arr.sort()` Sorts the elements of an array in place and returns the array.
401
-
-`array.splice(start, deleteCount, item1, item2)` Adds and/or removes elements from an array.
401
+
-`array.splice(start, deleteCount, element1, element2)` Adds and/or removes elements from an array.
402
402
-`arr.unShift("element1", "element2");` Adds one or more elements to the front of an array and returns the new length of the array.
403
403
404
+
### Example of arrray.sort()
405
+
406
+
```javascript
407
+
// Assume we have an array of objects with a 'category' property
408
+
constitems= [
409
+
{ name:'Banana', category:'Fruit' },
410
+
{ name:'Carrot', category:'Vegetable' },
411
+
{ name:'Apple', category:'Fruit' },
412
+
{ name:'Lettuce', category:'Vegetable' },
413
+
{ name:'Orange', category:'Fruit' }
414
+
];
415
+
416
+
// Sort the items by category in ascending order
417
+
items.sort((a, b) => {
418
+
if (a.category<b.category) {
419
+
return-1;
420
+
}
421
+
if (a.category>b.category) {
422
+
return1;
423
+
}
424
+
return0;
425
+
});
426
+
427
+
console.log(items);
428
+
429
+
// Outputs:
430
+
[
431
+
{ name:'Banana', category:'Fruit' },
432
+
{ name:'Apple', category:'Fruit' },
433
+
{ name:'Orange', category:'Fruit' },
434
+
{ name:'Carrot', category:'Vegetable' },
435
+
{ name:'Lettuce', category:'Vegetable' }
436
+
]
437
+
```
438
+
404
439
### Acessor Methods
405
-
-`arr.at(index)` Returns the element at the specified index in the array.
406
-
-`arr.concat(value1, value2, array2)` Returns a new array comprised of this array joined with other array(s) and/or value(s).
407
-
-`arr.includes(searchElement, fromIndex)` Determines whether an array contains a certain element, returning true or false as appropriate.
408
-
-`arr.indexOf(searchElement[, fromIndex])` Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
409
-
-`arr.join(separator)` Joins all elements of an array into a string.
410
-
-`arr.lastIndexOf(searchElement, fromIndex)` Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
411
-
-`arr.slice(begin, end)` Extracts a section of an array and returns a new array.
412
-
-`arr.toString()` Returns a string representing the array and its elements.
440
+
-`array.at(index)` Returns the element at the specified index in the array.
441
+
-`array.concat(value1, value2, array2)` Returns a new array comprised of this array joined with other array(s) and/or value(s).
442
+
-`array.includes(searchElement, fromIndex)` Determines whether an array contains a certain element, returning true or false as appropriate.
443
+
-`array.indexOf(searchElement[, fromIndex])` Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
444
+
-`array.join(separator)` Joins all elements of an array into a string.
445
+
-`array.lastIndexOf(searchElement, fromIndex)` Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
446
+
-`array.slice(begin, end)` Extracts a section of an array and returns a new array.
447
+
-`array.toString()` Returns a string representing the array and its elements.
413
448
- Overrides the Object.prototype.toString() method.
414
-
-`arr.toLocaleString(locales, options)` Returns a localized string representing the array and its elements.
449
+
-`array.toLocaleString(locales, options)` Returns a localized string representing the array and its elements.
415
450
- Overrides the `Object.prototype.toLocaleString()` method.
-`arr.every(callback[, thisArg])` Returns true if every element in this array satisfies the provided testing function.
419
-
-`arr.filter(callback[, thisArg])` Creates a new array with all of the elements of this array for which the provided filtering function returns true.
420
-
-`arr.find(callback[, thisArg])` Returns the found value in the array, if an element in the array satisfies the provided testing function or undefined if not found.
421
-
-`arr.findIndex(callback[, thisArg])` Returns the found index in the array, if an element in the array satisfies the provided testing function or -1 if not found.
422
-
-`arr.forEach(callback[, thisArg])` Calls a function for each element in the array.
423
-
-`arr.keys()` Returns a new Array Iterator that contains the keys for each index in the array.
424
-
-`arr.map(callback[, initialValue])` Creates a new array with the results of calling a provided function on every element in this array.
425
-
-`arr.reduce(callback[, initialValue])` Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
426
-
-`arr.reduceRight(callback[, initialValue])` Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.
427
-
-`arr.some(callback[, initialValue])` Returns true if at least one element in this array satisfies the provided testing function.
428
-
-`arr.values()` Returns a new Array Iterator object that contains the values for each index in the array.
465
+
-`array.every(callback[, thisArg])` Returns true if every element in this array satisfies the provided testing function.
466
+
-`array.filter(callback[, thisArg])` Creates a new array with all of the elements of this array for which the provided filtering function returns true.
467
+
-`array.find(callback[, thisArg])` Returns the found value in the array, if an element in the array satisfies the provided testing function or undefined if not found.
468
+
-`array.findIndex(callback[, thisArg])` Returns the found index in the array, if an element in the array satisfies the provided testing function or -1 if not found.
469
+
-`array.forEach(callback[, thisArg])` Calls a function for each element in the array.
470
+
-`array.keys()` Returns a new Array Iterator that contains the keys for each index in the array.
471
+
-`array.map(callback[, initialValue])` Creates a new array with the results of calling a provided function on every element in this array.
472
+
-`array.reduce(callback[, initialValue])` Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
473
+
-`array.reduceRight(callback[, initialValue])` Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.
474
+
-`array.some(callback[, initialValue])` Returns true if at least one element in this array satisfies the provided testing function.
475
+
-`array.values()` Returns a new Array Iterator object that contains the values for each index in the array.
476
+
477
+
### Example of array.reduce(callback[, initialValue])
478
+
479
+
```javascript
480
+
// Suppose we have an array of numbers
481
+
constnumbers= [1, 2, 3, 4, 5];
482
+
483
+
// We want to find the sum of all numbers in the array
0 commit comments