preface

“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

Less code makes it easier to read ~ let’s work together!

The body of the

Wait for multiple asynchronous functions

Have you ever had to call multiple interfaces to get an option in your daily development process?

async fun1() {
    const res = await http(url1, query2)
}

async fun2() {
    const res = await http(url2, query2)
}

fun1()
fun2()
Copy the code

We can await multiple async functions with promise.all, for example:

async fun() {
    await promise.all([fun1, fun2])
}
Copy the code

Format to display JSON code

Json.stringify can convert Javascript values to JSON strings, but does not know that it has other parameters? If so, check out the introduction below, and skip the reverse

The syntax for json.stringify is:

JSON.stringify(value[, replacer[, space]])

// Parameter description
// @param value What to convert
// @param replacer the function or array used to transform the result
    // function: json.stringify is called with the return value of the function instead of the original value. If the function returns undefined, the member is excluded. The key of the root object is an empty string: ""
    // Array: Only the members of the array that have keys are converted in the same order as the keys in the array
// @param space (emphasis) Text adds indentation, Spaces, and newlines.
    // Space is a number, so the return value text is indented by a specified number of Spaces at each level
    // If space is greater than 10, the text is indented 10 Spaces
    // Space can also use non-numerals, such as: \t
Copy the code

Therefore, json.stringify can not only char an object, but also format the output

Having covered the basics, let’s take a look at case studies:

const people = {
    girl: {
        age: 18.weight: 100
    },
    boy: {
        age: 18.weight: 150}}console.log(JSON.stringify(people, null.4))
/ / {
// "girl": {
// "age": 18,
// "weight": 100
/ /},
// "boy": {
// "age": 18,
// "weight": 150
/ /}
// }
Copy the code

The string number is quickly converted to the number type

If you’re still upset that the browser’s address bar refreshes and your ID is now a string, you can try this:

const a = '1'

console.log(typeof +a) // number
Copy the code

Fast conversion of string types

const a = 1 + ' '

console.log(typeof a) // string
Copy the code

Use the browser as an editor

Remember the contenteditable property? When this property is set, the content of the element becomes editable:

Put the following code into the console and you’ll be amazed to see the entire page become editable.

document.body.contentEditable='true'
Copy the code

No third party is required to complete the exchange of variables

We were often asked about bubble sorting in the interview before, and the normal logical code is:

function numbersSort(arr = []) {
    var len = arr.length;
    for (var i = 0; i < len - 1; i++) {
        for (var j = 0; j < len - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                // Compare adjacent elements in pairs, use a third party variable to temporarily store data when the elements are swapped, and finally replace the original data with this variable
                var temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp; }}}return arr;
}
Copy the code

Third party interference is often not what we want, so can we avoid it, of course, please see the following implementation:

function numbersSort(arr = []) {
    var len = arr.length;
    for (var i = 0; i < len - 1; i++) {
        for (var j = 0; j < len - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                // Data exchange can be realized in a single sentence without the need for a third party
                arr[j]=[arr[j + 1], arr[j + 1]= arr[j]][0]}}}return arr;
}
Copy the code

Conclusion:

Everybody got it? Let’s look forward to the next update