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
Copy file name to clipboardExpand all lines: 1-js/05-data-types/03-string/3-truncate/task.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ importance: 5
6
6
7
7
Create a function `truncate(str, maxlength)` that checks the length of the `str` and, if it exceeds `maxlength` -- replaces the end of `str` with the ellipsis character `"…"`, to make its length equal to `maxlength`.
8
8
9
-
The result of the function should be the truncated (if needed) string.
9
+
The result of the function should be the truncated (if needed) string.
In the call above, we use `user.younger` as a filter and also provide `user` as the context for it. If we didn't provide the context, `users.filter(user.younger)` would call `user.younger` as a standalone function, with `this=undefined`. That would mean an instant error.
673
673
674
-
## Other methods
674
+
## Summary
675
+
676
+
A cheatsheet of array methods:
677
+
678
+
- To add/remove elements:
679
+
-`push(...items)` -- adds items to the end,
680
+
-`pop()` -- extracts an item from the end,
681
+
-`shift()` -- extracts an item from the beginning,
682
+
-`unshift(...items)` -- adds items to the beginning.
683
+
-`splice(pos, deleteCount, ...items)` -- at index `pos` delete `deleteCount` elements and insert `items`.
684
+
-`slice(start, end)` -- creates a new array, copies elements from position `start` till `end` (not inclusive) into it.
685
+
-`concat(...items)` -- returns a new array: copies all members of the current one and adds `items` to it. If any of `items` is an array, then its elements are taken.
686
+
687
+
- To search among elements:
688
+
-`indexOf/lastIndexOf(item, pos)` -- look for `item` starting from position `pos`, return the index or `-1` if not found.
689
+
-`includes(value)` -- returns `true` if the array has `value`, otherwise `false`.
690
+
-`find/filter(func)` -- filter elements of through the function, return first/all values that make it return `true`.
691
+
-`findIndex` is like `find`, but returns the index instead of a value.
692
+
693
+
- To transform the array:
694
+
-`map(func)` -- creates a new array from results of calling `func` for every element.
695
+
-`sort(func)` -- sorts the array in-place, then returns it.
696
+
-`reverse()` -- reverses the array in-place, then returns it.
697
+
-`split/join` -- convert a string to array and back.
698
+
-`reduce(func, initial)` -- calculate a single value over the array by calling `func` for each element and passing an intermediate result between the calls.
699
+
700
+
- To iterate over elements:
701
+
-`forEach(func)` -- calls `func` for every element, does not return anything.
675
702
676
-
We covered the most useful methods. But there are few others:
703
+
- Additionally:
704
+
-`Array.isArray(arr)` checks `arr` for being an array.
705
+
706
+
Of all these methods only `sort`, `reverse` and `splice` modify the array itself, the other ones only return a value.
707
+
708
+
These methods are the most used ones, they cover 99% of use cases. But there are few others:
677
709
678
710
-[arr.some(fn)](mdn:js/Array/some)/[arr.every(fn)](mdn:js/Array/every) checks the array.
679
711
@@ -683,23 +715,10 @@ We covered the most useful methods. But there are few others:
683
715
684
716
-[arr.copyWithin(target, start, end)](mdn:js/Array/copyWithin) -- copies its elements from position `start` till position `end` into *itself*, at position `target` (overwrites existing).
685
717
686
-
These and other methods are also listed in the [manual](mdn:js/Array).
687
-
688
-
689
-
## Summary
690
-
691
-
Most used array methods:
718
+
For the full list, see the [manual](mdn:js/Array).
692
719
693
-
-`split/join` -- convert a string to array and back.
694
-
-`splice` -- delete and insert elements at the given position.
695
-
-`sort` -- sorts the array.
696
-
-`indexOf/lastIndexOf`, `includes` -- look for the value.
697
-
-`find/filter` -- return first/all values satisfying the given condition, `findIndex` is like `find`, but returns the index instead of a value.
698
-
-`forEach` -- runs a function for each element.
699
-
-`map` -- transforms the array through the function.
700
-
-`reduce/reduceRight` -- calculates a single value based on array.
701
-
-`slice` -- copy the part of the array.
720
+
From the first sight it may seem that there are too many methods, difficult to remember. But you have to.
702
721
703
-
These methods are used in 95% of cases. For the full list, see the [manual](mdn:js/Array).
722
+
Look through the cheatsheet just to be aware of them. Then do the tasks of this chapter to practice, so that you have experience with most methods.
704
723
705
-
The methods are easy to remember when you use them. Please refer to the tasks for some practice.
724
+
Afterwards whether you need to do something with an array, and you don't know how -- come here, look at the cheatsheet and find the right method. Examples will help you to write it correctly. Soon you'll automatically remember the methods, without specific efforts from your side.
0 commit comments