This is the 7th day of my participation in Gwen Challenge

JSON expand

parse

Parse (STR, fn) parse(STR, fn)

STR: string to process

Fn: function to be processed

The return value is the result of this processing

There are two parameters

The first parameter: the property name

The second parameter: the property value

Traversal from the outside to the inside

For example:

// Define a JSON string
var str = '{"a": 1, "b": "2", "c": {"d": 3}}';

// Turn STR into a js object
var result = JSON.parse(str, function(key, value) {
	console.log(arguments);
	// Determine the attribute value
	if (typeof value === "string") {
		return +value;
	}
	return value;
});
Copy the code

Output:

// We want to convert strings to numbers
console.log(result);
Copy the code

Results:

stringify

This method is used to convert a JS object to a JSON string

Json.stringify (obj, fn)

Obj: Object to be processed

Fn: processing function

The return value is the result of this processing

There are two parameters

The first parameter: the attribute name

The second parameter: the property value

Traversal from the inside to the outside

For example:

// Define objects
var obj = {
	a: 1.b: "2".c: {
		d: 3}}// Convert obj to a JSON string
var result = JSON.stringify(obj, function(key, value) {
	console.log(arguments);
	// We want to keep all attribute values as numbers
	// Evaluate value
	if (typeof value === "string") {
		return +value;
	}
	return value;
});
Copy the code

Output:

console.log(result);
Copy the code

Results:

An array of development

Judge array

The first way to determine the object type is an array

Object.prototype.toString.call(obj)

The second way to determine if the constructor is Array

obj.constructor === Array

The third way to determine if it is an instantiated object

obj instanceof Array

The fourth way is to determine the static method isArray

Array.isArray(obj)

For example:

// The first instanceof
console.log(arr instanceof Array);
// The array is correct, but the constructor Object is also true
console.log(arr instanceof Object);
console.log(obj instanceof Array);

// The second kind of constructor
console.log(arr.constructor === Array);
console.log(arr.constructor === Object); // false
console.log(obj.constructor === Array);

// The third toString
console.log(Object.prototype.toString.call(arr) === "[object Array]");
console.log(Object.prototype.toString.call(obj) === "[object Array]");

// The fourth static method of an array
console.log(Array.isArray(arr));
console.log(Array.isArray(obj));
Copy the code

Gets the index value of the member

ES5 extends two methods for arrays: indexOf and lastIndexOf, which are used to retrieve the index value of each member of an array

The parameter is the member to look up

The return value is the index value of the member

If no member is found, -1 is returned

When searching for members, there is no data type conversion, it is true congruent search

For example:

/ / compatible with Internet explorer
// The instantiated object has methods that should be on the class's prototype
if (!Array.prototype.indexOf) {
	// extend the method
	Array.prototype.indexOf = function(item) {
		// Iterate over this
		for (var i = 0; i < this.length; i++) {
			Arr [I] indicates the member value
			// Check whether arr[I] is congruent with item
			if (arr[i] === item) {
				// If a member is found, the index value is returned
				returni; }}// Return -1 if no trace is found
		return -1; }}Copy the code

forEach

This method replaces the for loop and is an array iterator method. It does not remove the for loop, but wraps the loop inside the array iterator method forEach.

ForEach (fn)

Fn: function to be executed

There are three parameters:

The first parameter: the member value

Second parameter: index value

Third argument: primitive array

The scope is Window

The return value of the function has no effect on the result of the forEach execution

ForEach always returns undefined

In jQuery, the first parameter of each method is the index value, and the second parameter is the member value

map

This method is used to iterate over a set of numbers and map the results, similar to the forEach method, except that its return value is meaningful

Arguments are functions to be executed

The function takes three arguments: a member value, an index value, and a primitive array

The scope is Window

The return value is the array member that executes the result

The map method returns a new array whose members are the results of each function execution

For example:

/ / compatible with Internet explorer
if (!Array.prototype.map) {
	// extend the method
	Array.prototype.map = function(fn) {
		// Create an array result container
		var result = [];
		// go through the number group
		for (var i = 0; i < this.length; i++) {
			// execute fn and pass the argument
			// Pass three arguments: the member value this[I], the index value I, and the array this
			result.push(fn(this[i], i, this));
		}
		// Returns a new array
		returnresult; }}Copy the code

fill

When we use new Array(len) or Array(len) to create an Array, we get only the length and no members, so we can’t use the Array iterator method (forEach, map), we have to fill the Array

Arguments are just the members to fill in and even functions don’t execute them

For example:

/ / compatible with Internet explorer
if (!Array.prototype.fill) {
	// extend the method
	Array.prototype.fill = function(item) {
		// go through the number group
		for (var i = 0; i < this.length; i++) {
			// Populate the array
			this[i] = item;
		}
		// Returns the populated array
		return this; }}Copy the code

some

Whether any member of the array meets the criteria

It is used in a similar way to forEach

Arguments are functions to be executed

There are three parameters: the member value, the index value, and the primitive array

The return value is what counts

The return value of the some method:

True: At least one member meets the condition

False: The conditions are not met

The some method is sensitive to true and stops traversal as soon as the condition is satisfied

For example:

/ / compatible with Internet explorer
if (!Array.prototype.some) {
	// extend the method
	Array.prototype.some = function(fn) {
		// Iterate over this
		for (var i = 0; i < this.length; i++) {
			// Execute the function and judge the result
			// Pass three arguments: the member value this[I], the index value I, and the array this
			if (fn(this[i], i, this)) {
				// If there are any
				return true; }}// If no member satisfies the condition, return false
		return false; }}Copy the code

every

Whether the conditions are met in all of the arrays

It is used in a similar way to forEach

Arguments are functions to be executed

Parameter has three parameters: member value, index value, and primitive array

The return value is what counts

The every method returns:

True: All conditions are met

False: Some member does not meet the condition

Every is sensitive to false and stops traversal as soon as it encounters one that does not meet the condition

For example:

/** * some determines whether any member of the array satisfies the condition *@arr The array to determine *@fn Function * RETURN bool Indicates whether any member meets the condition **/
function some(arr, fn) {
	// go through the number group
	for (var i = 0; i < arr.length; i++) {
		// Execute the function and judge the result
		Arr [I]; arr[I]; arr[I]
		if (fn(arr[i], i, arr)) {
			// Returns true if any member meets the criteria
			return true; }}// When traversal is complete, no member satisfies false
	return false;
}
Copy the code

filter

Implement the array filter

It is used in a similar way to forEach

Arguments are functions to be executed

The function takes three arguments: a member value, an index value, and a primitive array

The return value is the filter condition

The return value of the filter method is a new array of the members that meet the filter criteria

For example:

/** * implement the filter method *@arr The array to filter *@fn  The function * return [] is a new array of members that meet the filter criteria **/
 function filter(arr, fn) {
 	// Create a new empty array
        var result = [];
 	// go through the number group
 	for (var i = 0; i < arr.length; i++) {
 		// Execute the function and pass arguments
 		Arr [I]; arr[I]; arr[I]
 		if(fn(arr[i], i, arr)) { result.push(arr[i]); }}// Returns a new array
 	return result;
 }
Copy the code

Reduce, reduceRight

These two methods are cumulative. Reduce is cumulative from front to back, while reduce is cumulative from back to front. All members are processed one by one and the results are returned

Arguments are functions to be executed

It takes four parameters: the last cumulative result, the current member value, the current index value, and the original array

The return value is the cumulative result that will be passed as the first argument on the next iteration

Reduce is traversed from the second member, and the first member is passed as the first parameter in the first traversal

ReduceRight is traversed from the second-to-last member, and the penultimate member is used as the first parameter in the first traversal

For example:

/** * Implement the reduceRight method *@arr An array of *@fn  Function * return cumulative result **/
 function reduceRight(arr, fn) {
 	// Define cumulative results
 	// Since we are traversing from the second-to-last member, the last cumulative result should be the penultimate member
 	var result = arr[arr.length - 1];
 	// The traversal group starts with the next-to-last member
 	for (var i = arr.length - 2; i >= 0; i--) {
 		// Execute functions: last cumulative result, current member value, current index value, original array
 		result = fn(result, arr[i], i, arr);
 	}
 	// Return the result
 	return result;
 }
Copy the code

addNum

AddNum (num1, num2) method, accept two parameters, respectively are two integers, find the sum of all integers between two integers

addNum(1, 100)

It may or may not contain two arguments

We have two parameters

Do not use for loops

For example:

// Define the method
function addNum(num1, num2) {
	// Determine the maximum and minimum values
	var max = Math.max(num1, num2);
	var min = Math.min(num1, num2);
	// console.log(max, min);

	// (5, 10) 5, 6, 7, 8, 9, 10 10-5 + 1
	// (5, 10) 6, 7, 8, 9 10-5-1
	// Create an array
	return Array(max - min + 1)
	// To iterate through the array, populate the array
	.fill(1)
	// Build a new array between the minimum and maximum values
	.map(function(value, index, arr) {
		// Index is incremented by min + index to get an array ranging from minimum to maximum
		return min + index;
	})
	// find the summation method
	.reduce(function(pre, value) {
		returnpre + value; })}Copy the code