Friday, April 10, 2020

Full version of JavaScript for Loop Loop Guide


Take a few minutes to relive the basics of the JavaScript loop guide and remember a figure



During front-end development, we often use JavaScript to provide a lot of loop and iteration methods, commonfor, for... of,for... in,while, Array.forEach, as well as Array.* (There are also some Array methods similar to loop/iterator: Array.values (), Array.keys (), Array.map () , Array.reducer (), etc.), and these are very basic things, there must be people who think these are very basic. Something, what's good looking at? The purpose of these things is to be framed and brainized. So it's simple to make a diagram of a JS loop. Integrate cluttered, fragmented knowledge into everything, making it easier to remember thebasics.



Simple Loop
for
The for loop repeats as long as certain conditions are met. It is usually used to execute a code block a certain number of times.

array loop


const arr = [1, 2, 3]
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i])
}

break statement

Use break statement to terminate a loop


 for (  let i = 0 ;; i++) {
 console.log ( ' loop ')
 break
}

// output: loop

In this case, in the for loop, the break keyword exits the loop when it encounters the loop.

continue statement

The continue statement can be used to continue execution (skip the rest of the code block and go to the next loop)


 for (  let i = 0 ; i < 3; i++) {
 if (i === 2)  continue
 console.log (i)
}

// output: 0, 1


The above code can enter the result, when condition i === 2 is established, encounter continue keyword to skip and proceed to the next loop.

for…of

The for... of statement creates a loop on an iterable object (including Array, Map, Set, arguments, and so on), with each unique property of the value with one iteration.

string string loop



let string = 'text'

for (let value of string) {
  console.log(value) // output "t", "e", "x", "t"
}

array array loop



let arr = [0, 1, 2]
for (let value of arr) {
  console.log(value) // output 0, 1, 2
}

objects object loop

For... of loop works only for iterable values, objects are not iterable, so it is not feasible to loop objects directly using for... of. The following example:


let object = { a: 1, b: 2, c: 3 }

for (let value of object) // Error: object is not iterable
  console.log(value)

You can convert an object to an iterable object using the built-in Object method:.keys (),.values () or.entries (), see the following example:

let enumerable = { property : 1, method : () => {} };

for (let key of Object.keys( enumerable )) console.log(key);
> property
> method

for (let value of Object.values( enumerable )) console.log(value);
> 1
> () => {}

for (let entry of Object.entries( enumerable )) console.log(entry);
> (2) ["prop", 1]
> (2) ["meth", ƒ()]

It can also be achieved by using a for... in loop without using the built-in Object method.

for…in

A for... in loop is a special type of loop that traverses the properties of an object or the elements of an array. When traversing objects, you can display enumerable object properties


let object = { a: 1, b: 2, c: 3, method: () => {} }

for (let value in object) {
  console.log(value, object[value])
}

// output: 1, 2, 3, () => { }

while

A while statement executes its block as long as the specified condition evaluates to true (true).

 let c =  0

while (c++ < 5) {
 console.log (c)
}

// output: 1, 2, 3, 4, 5 

do... while

is very similar to while, while the do... while statement repeats until the specified condition evaluates to a false value (false).

 var i =  1
do {
 console.log (i)
 i++
} while (i <= 5)

// output: 1, 2, 3, 4, 5 

Arrays Loops



Array has several iteration methods. Usually we recommend using the built-in Array method for loop operations instead of using for or while loops. The array method is attached to the Array.prototype property, which means that it is used directly from the array object.

For example, usingArray.forEach ()method to manipulate an array

forEach

Definition: The forEach method executs the given function once on each element of the array.
Return value: none


let arr = ['jack', 'tom', 'vincent']

arr.forEach((name) => console.log(`My name is ${name}`))

//output
// My name is jack
// My name is tom
// My name is vincent

every

Definition: checks whether all elements of the array meet the judgment criteria, returns true, otherwise false
Return value: boolean


const isBelowThreshold = (currentValue = currentValue < 40)
const array1 = [1, 30, 39, 29, 10, 13]

console.log(array1.every(isBelowThreshold))

// output: true

 some



Definition: whether there are elements in the array that satisfy the judgment criteria. If at least one of the judgment conditions is satisfied, the return value isfalse ifnone are satisfied:
boolean

filter

Definition: Execres the given function once on each element of the array, returning a new array
Return value: New array


const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

map

Definition: The method creates a new array whose result is the return value after each element of the array has been called once.
Return value: New array


const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

reduce && reduceRight



Definition: Thereducemethod performs a reducer function (in ascending order) supplied by you on each element of the array, summarising its results as A single return value.


const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15


Definition:reduceRightmethod accepts a function as accumulator and each value of an array (right to left) ) to reduce it to a single value.


const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(
  (accumulator, currentValue) => accumulator.concat(currentValue)
);

console.log(array1);
// expected output: Array [4, 5, 2, 3, 0, 1]

find()& findIndex()



Definition: Thefind method is used to find the first array member that matches the condition, and returns the member if there are no members that match the condition undefined.


const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12


Definition:findIndexreturns the position of the first array member that matches the condition, or -1 if all members do not match the condition.


const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3


The Git branch development specification you must know

Git is currently the most popular source control tool.
For specification development, keep code commit records and git branch structure clear, convenient for subsequent maintenance, now regulate git related operations.


Branch Naming

master branch


  • Master is the master branch, which is also used to deploy production environment, ensuring the stability of master branch
  • Master branches are generally incorporated by development and hotfix branches, you cannot modify the code at any time.

develop branch

  • develop is a development branch, always keeping the latest finished and bug-fixed code
  • In general, feature branches are created under the development branch
feature branch

  • Create feature branches based on development when developing new features
  • Branch naming: feature/beginning with feature branches, naming rules: feature/user_module, feature/ cart_module
release branch

  • release is the pre-line branch, release the prediction phase, will release the branch code for the baseline prediction

When a set of features is developed, it will first merge into the development branch, and when it enters the prediction, it will Create areleasebranch.
If there are bugs that need to be fixed during the testing process, the developer will use the The branch is fixed and committed.
When the test is complete, merge thereleasebranch into the masterand develop branches, at this point Masteris the latest code for online use. 

hotfix branch


  • Branch naming: hotfix/beginning with fix branch, its naming rules are the same as feature


  •  branch is similar to If there is an emergency problem online, you need to fix it in time, using master branch as baseline, create a hotfix branch, after the fix is complete, you need to merge into master branch and develop branch

Common Tasks
Add new features


( dev) $: git checkout -b feature/xxx # Create feature branch from dev
(feature/xxx) $: blabla # development
(feature/xxx) $: git add xxx
(feature/xxx) $: git commit -m 'commit comment'
(dev) $: git merge feature/xxx —no-ff  # incorporate feature branches into dev

Fix emergency bugs 



( master) $: git checkout -b hotfix/xxx # Create a hotfix branch from master
(hotfix/xxx) $: blabla # development
(hotfix/xxx) $: git add xxx
(hotfix/xxx) $: git commit -m 'commit comment'
(master) $: git merge hotfix/xxx —no-ff  # Combine the hotfix branch into master and go online to production
(dev) $: git merge hotfix/xxx —no-ff  # merge the hotfix branch into dev, synchronize the code

Test Environment Code 



 ( release) $: git merge dev —no-ff # merge dev branch into release, then pull and test in the test environment 

Production environment online 



( master) $: git merge   release —no-ff # Combining release test code into master, operation and maintenance personnel
(master) $: git tag -a v0 .1 -m 'deployment Package version name ' #给版本命名,打Tag  

 Log specification

In a teamwork project, the developer often needs to submit code to fix bugs or implement new features. The files in the project, what features are implemented and what problems are solved, will be lost and time spent reading the code. But good log specification commit messages writing helps us, and it also reflects whether a developer is a good collaborator.

Writing good Commit messages can achieve 3 important purposes:


  • Accelerate the review process


  • Help us write good release log



  • To let the later defenders understand the specific changes in the code and why features are added
Currently, the community has a variety of writing standards for Commit message. The Angular specification is currently the most widely used writing, more rational and systematic. As shown in the following figure:


Basic syntax for Commit messages


Angular Git Commit Guidelines is widely used in the industry.

The format is:

<type>: <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

  • type: The type of commit, such as bugfix docs style, etc.
  • scope: Scope of this commit
  • subject: The main thrust of this commit is briefly explained. In the original text, several points were specifically emphasized 1. Using a praying sentence, is not very familiar and unfamiliar word, to send the door in this praying sentence 2. Do not uppercase the first letter 3. No need to add a mocking at the end
  • body: We need to describe this commit in detail, such as the motive of this change, if you need to enter a line, use |
  • footer: Describe the issue or break change associated with it, see the case
Category description for Type:



  • feat: adding new features
  • fix: fixing bugs
  • docs: only modified documentation
  • style: Just modify spaces, formatting indents, all good, etc., do not change the code logic
  • refactor: code refactoring, no new features or bug fixes
  • perf: Increase code for performance testing
  • test: Add test cases
  • chore: Change build flow, or add dependency libraries, tools, etc.
Commit messages format requirements



# Title line: 50 characters , describing the main changes
#
# Body: More detailed description text, recommended 72 characters or less. Information to be described includes:
#
# Why is this change required? It may be used to fix a bug, add a feature, improve performance, reliability, stability, etc.
# How does he solve this problem? Describe the steps to resolve the problem
#* Are there side effects, risks? 
#
# If desired, you can add a link to the issue address or other documents