These tips became treasures when I found out about them
1. Sum all the values from an array
Suppose we have an array of numbers:
let numbers = [2,52,55,5].
To get the sum we usually use a
for loop and traverse through the list right
You can easily do that with this line of code:
let sum = numbers.reduce((x,y) => return x+y).
And you can print the result via
2. Reduce the length of an array using an array length property
We can reduce the size of the array using the
Let's see the code for that:
let array = [11,12,12,122,1222]
We now have an array of 5 elements.
array.length will return 5.
Now suppose I want to reduce the length of an array we can just do it by using
array.length = 4 now when you print the array your array will consist of
3. Shuffle array elements
We all need to get random data sometimes. But sometimes we need to get random data from a specific dataset.
At that time we can use the below snippet that will save your time:
4. Filter unique values
Sometimes we need to filter unique values right. For example, if you are on social media, we have mutual friends. Those mutual fronts are the negation of non-mutual friends i.e unique friends.
For that, we are using
A set is a collection of well-defined data ie, non-empty, dissimilar, unique.
5. Comma Operator
The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand.
6. Swap values with array destructuring
Swapping values has never been easy like this, usually, we introduce a
temporary variable then
temporary = b; b = a; a = temporary;. But that produces a headache, right?
Well, now you can just swap using array destructuring:
7. Replace if true statements with &&
&& operators are less often used but now you will use more often.
We can make it shorter by using && operator:
- && conditional
- Random of Specific
- Adding/summing up an array
- Reducing array
- Comma operator
- Filtering Unique value
Build composable web applications
Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favorite frameworks like React or Node. Build scalable frontends and backends with a powerful and enjoyable dev experience.
Bring your team to Bit Cloud to host and collaborate on components together, and greatly speed up, scale, and standardize development as a team. Start with composable frontends like a Design System or Micro Frontends, or explore the composable backend. Give it a try →
How We Build Micro Frontends
Building micro-frontends to speed up and scale our web development process.
How we Build a Component Design System
Building a design system with components to standardize and scale our UI development process.
The Composable Enterprise: A Guide
To deliver in 2022, the modern enterprise must become composable.