Array.length
Setting the length property changes the array size. If the value is set to a value smaller than its current value, the array is truncated and its trailing elements are lost. If the value is greater than its current value, the array is enlarged, and new elements are added to the end of the array with a value of undefined. This means that you can use length-1 instead of substring,splice, etc.Copy the code
Array. Every and Array. Some
These two methods may not be often used by many students. For array objects, there are many ways to iterate, such as each,filter,reduce, etc. What are these two methods used? Tests whether all elements of an array pass the test for the specified function. Neither method changes the structure of the original arrayCopy the code
Syntax: arr.every(callback, [thisArg])
The first argument is the callback function, and the second argument replaces the reference to this in callback. Callback is called with three arguments: the element value, the element index, and the original array. This is pretty much the usual iterative approach. Example: The following example checks whether all elements in an array are greater than 0
function isBigThenZero(element, index, array) {
return(element > 0);
}
var zero = [-12, 5, -8, 13, 4].every(isBigThenZero); // passed is false
zero = [12, 54, 18, 1, 4].every(isBigThenZero); // passed is true
Every returns true if every element in the array meets the criteria, while some returns true if every other element in the array meets the criteria. We can see from these two methods that we can use when we need to do some specific validation on an array element without making any changes to the array. If we choose to use each and for loops for validation, we may also need to introduce intermediate variables, which are not as neat as this.
Enumerability of an object
for… In loop: Only the enumerable properties of the object itself and its inheritance are iterated over.
Keys () : Returns the key names of all the enumerable properties of the Object itself.
Json.stringify () : Serializes only enumerable properties of the object itself.
Object.assign() : Ignore enumerable as false and copy only the enumerable properties of the Object itself.
JSON
Json.stringify (value,[,replacer[,space]]) this serialization method takes three arguments. The middle argument is used for serialization, and the third argument can be used to beautify the output string by adding two Spaces to the indentation
MDN explanation:
value
The value to be serialized into a JSON string.
Replacer optional
If the parameter is a function, each attribute of the serialized value is converted and processed by the function during serialization; If the parameter is an array, only the property names contained in the array will be serialized into the final JSON string; If this parameter is null or not provided, all attributes of the object are serialized; For a more detailed explanation and example of this parameter, see the article Working with Native JSON Objects.
Space optional
Specifies a blank string for indentation, which is used to beautify the output (pretty-print). If the argument is a number, it represents how many Spaces there are; The upper limit is 10. If the value is less than 1, there are no Spaces. If the argument is a string (the first ten letters of the string), the string is used as a space; If the parameter is not provided (or is null) there will be no Spaces.
forEach
ForEach does not terminate the loop until it reaches the end element. An internal return simply breaks out of the loop. If you must break out of the loop, you can terminate the loop by throwing an exception.
Continue to add in……