Summary of Common Methods of JS Objects

1. charAt()

Returns the character at the specified position

var str = 'nihaome wozhendexiangxeuhaoqianduan'
var str2 = str.charAt(4); The subscript of a string starts at 1.,So go back o
var str3 = str.charAt(25);  25 Location is a;
var str4 = str.charAt(5,26);  Two parameters are passed in without error, but the second parameter is invalid by default, so the character at position 5 is returned. m

2. charCodeAt()

Unicode encoding of characters returning to the specified location

var str = "llinihao mezaigama";
var str2 = str.charCodeAt(5);  The character at position 5 is h,h Of Unicode The code is 104.
var str3 = str.charCodeAt(3);
console.log(str3); // 110
console.log(str2); // 104

3. conca()

Connection string

var str1 = "lixiaoqi";
var str2 = 'zaijian';
console.log(str2.concat(str1)); hold str1 connection to str2 upper 
// zaijianlixiaoqi

4. indexOf()

Retrieves a string, returning the subscript of the character in the string

var str1 = "lixiaoqi";
var num1 = str1.indexOf('o'); retrieves the string and returns the subscript of the character in the string
 The subscript of a string counts from zero
 var num2 = str1.indexOf('lllll'); no matching value is found, return - 1
 var num3 = str1.indexOf('ni'); no matching value is found, return - 1
console.log(num1); // 5
console.log(num2); // -1
console.log(num3); // -1

5. match()

Retrieving a specified value within a string or finding a match of one or more regular expressions returns the location of the value rather than the value.

var str1 = "lixiaoqi";
var str2 = str1.match('iao');  Retrieve a string, returning a character
var str3 = str1.match('lll');  No specified character is returned. null

6. replace()

Replace matched strings

var str1 = "nihaome";
var str2 = "lixiaoqi";
var str3 = str1.replace(str1.match('hao'),str2.match('lixi'));
//Find hao for str1, lixi for str2, and replace STR1 with STR2
console.log(str3); // nilixime

7. search()

Retrieves a substring that matches a string and returns the address

var str1 = "lixiaoqi nihaoya zaiganma";
var str2 = "nihaoya";
var str3 = "wobuzai";
var num1 = str1.search(str2); Returns the location of the first character.
var num2 = str1.search(str3); No return detected-1
console.log(num1); // 9
console.log(num2); // -1

8. split()

Divide characters into arrays

var str1 = "nihao-zaijian-dajiahao";
console.log(str1.split('')); // ["n","i","h","a","o","z","a" ...]
var str2 = str1.split('-');
console.log(str2); // ["nihao","zaijian","dajiahao"]
console.log(str1); The original string remains unchanged  //  nihao-zaijian-dajiahao

9. slice()

Extract string fragments and return the extracted part in the new string

var str1 = 'nihaoya wobuhaoya'
console.log(str1.slice(4,9)); 4 Position begins and ends at the front of 9.
console.log(str1.slice(2,14)); start The position begins. end The former position ends
console.log(str1); The original string remains unchanged
console.log(str1.slice(-1,0));  Returns an empty string
console.log(str1.slice(30,100));  Returns an empty string if the length exceeds the length of the string
console.log(str1.slice(-1,10)); Returns an empty string

10. toLocaleLowerCase()

Converting strings to lowercase

var str = "NIHAOME WOHENHAOnizaiganmane";
console.log(str.toLocaleLowerCase()); // nihaome wohenhaonizaiganmane
var str1 = str.toLocaleLowerCase(); 
console.log(str1);  // nihaome wohenhaonizaiganmane
console.log(str); The original string remains unchanged  // NIHAOME WOHENHAOnizaiganmane

11. toLowerCase()

Converting strings to lowercase

var str = "deawwWWEzaiganmane";
console.log(str.toLowerCase()); // deawwwwezaiganmane
var str1 = str.toLowerCase();
console.log(str1);  // deawwwwezaiganmane
console.log(str); The original string remains unchanged

12. toLocaleUpperCase()

Quasi-capitalization of strings

var str = "deawwWWEzaiganmane";
console.log(str.toLocaleUpperCase()); // DEAWWWWEZAIGANMANeE
var str1 = str.toLocaleUpperCase();
console.log(str1);  // DEAWWWWEZAIGANMANeE
console.log(str); The original string remains unchanged

13. substr()

Extract the character of the specified Bibliography from the starting index number

var str = "nihaoya wohenhao dajiahao"
var str3 = str.substr(3,19); Spaces do not occupy positions in strings, starting at 3 and ending at 19
console.log(str3); // aoya wohenhao dajia

15. subString()

Extract characters between two specified index numbers in a string

var str = "nihaoya wohenhao dajiahao"
var str3 = str.substring(3,19);Space occupies a position in a string, starting at 3 and ending at 18
var str4 = str.substr(3,19);No space is occupied. From 3 to 19

Array array

1. slice[start,end]

Returns a new array of items from the original array specified to start the following table to end the following table (the original array remains unchanged)

var arr = [1,2,3,4,5,6,7,8];
var arr1 = arr.slice(2,6);
console.log(arr1); // ["3","4","5","6"]
The table below the array starts at 0, starts at 2, and ends at the previous position of 6.
var arr3 = arr.slice(4); from 4 to end
console.log(arr3); // ["5","6","7","8"]

2. splice()

var arr = [1,2,3,4,5];
var arr1 = arr.slice(2,3); the actual number of items in the new array is end-strat;
console.log(arr1); // [3]
var arr2 = arr.slice(1,5);
console.log(arr2); // [2,3,4,5]
console.log(arr); // [1,2,3,4,5]
Insert: 3 parameters, starting position, number of items deleted, items inserted.
Replacement, Arbitrary Parameters, Starting Position, Number of Items Deleted, Number of Items Inserted

3. pop()

Delete the last element of the array, reduce the length of the array, and return the deleted value

var arr = [1,2,3,4,5];
console.log(arr.length); // 5
var arr1 = arr.pop(); 
console.log(arr1); //5
console.log(arr.length);  //4

4. push()

Add parameters to the end of the array and return the length of the new array

var arr = [1,2,3,4,5];
console.log(arr.length);  // 5
var num = arr.push(6,7,8); 
console.log(num); Returns the length of the new array // 8
console.log(arr); The original array is changed to a new array // [1,2,3,4,5,6,7,8];

5. shift()

Delete the first parameter of the array, reduce the length of the array by 1, no parameter

var arr = [1,2,3,4,5];
console.log(arr.length); // 5
var arr1 = arr.shift(); 
console.log(arr1); Returns the value of the deleted array  // 1
console.log(arr); The original array is changed to a new array // [2,3,4,5];

6. unshift()

Add one or more elements like the beginning of an array and return a new length. (parameters are not limited)

var arr = [1,2,3,4,5];
console.log(arr.length); // 5
var arr1 = arr.unshift(2,4,5,{name:'liqi'}); 
console.log(arr1); Returns the length of the new array // 9
console.log(arr); The original array is changed to a new array // [2,4,5,{name: liqi},1,2,3,4,5];

7. sort()

Sort arrays by specified parameters. The returned values are sorted arrays (no parameters, functions)

var arr = [1,'q',3,6,2,'a',9];
console.log(arr); Primitive array
var arr1 = arr.sort();
console.log(arr1); Returns a sorted array // [1,2,3,6,9,"a","q"];
console.log(arr); The original array becomes a new array  // [1,2,3,6,9,"a","q"];

8. concat(3,4)

Connect the two strings and return a copy of the value (with unlimited parameters)

var arr1 = [1,'q',3,6,2,'a',9];
var arr2 = [{name:'niho'},1,4];
console.log(arr1.concat(3,arr1));Create a new array with the original array unchanged // [1,"q",3,6,2,"a",9,3,1,"q",3,6,2,"a",9]
console.log(arr1); // [[1,"q",3,6,2,"a",9]
console.log(arr2);The original array remains unchanged  //[{name:'niho'},1,4];

9. indexOf()

Look backwards from the beginning of the array, accepting two parameters, the item to be looked up and the location index of the starting point to be looked up

var arr = [1,2,3,4,5,6,78];
var num = arr.indexOf(78,3); Find the location of 78
console.log(num); Index of returned items // 6
var num1 = arr.indexOf(3,1);
console.log(num1); // 2
var num2 = arr.indexOf(2);
console.log(num2); // 1
console.log(arr); // [1,2,3,4,5,6,78];

10. join()

Compose the elements of an array into a string to separate or

var arr1 = [1,'q',3,6,2,'a',9];
var arr2 = arr1.join(','); Separated by commas
var arr3 = arr1.join(''); Separated by spaces
console.log(arr1); The original array remains unchanged
console.log(arr2); // 1,q,3,6,2,a,9
console.log(arr3); // 1q362a9

11. forEach()

Perform the provided function once for each element of the array.

var arr1 = ['a', 'b', 'c'];
arr1.forEach(function(element) {
  console.log(element); // a b c
});

12. map()

Running a given function for each item of an array returns an array of the results of a funny function call.

var arr = [1, 4, 9, 16];
const map1 = arr.map(x => x * 2); //For each item of the array*2
console.log(map1); // 2 8 18 32

13. for in method

foreach

var arr = [1, 4, 9, 16];
for (var i = 0; i < arr.length; i++){
    console.log(arr[i]); // 1 4 9 16
}

ES5-every and some methods

  1. Every is true when every callback of all functions returns true, and terminates execution when it encounters false and returns false.
  2. some functions terminate execution and return true when a function returns true, otherwise return false.
// every method
var arr = [1,6,8,-2,-5,7,-4]
var isPositive = arr.every(function(value){
    return value > 0;
})
console.log(isPositive) // false

//some method
var arr = [1,6,8,-2,-5,7,-4]
var isPositive = arr.some(function(value){
    return value > 0;
})
console.log(isPositive) // true

ES5-Array.isArray(obj)

This is a static function of an Array object to determine whether an object is an array or not.

var arr = [3,4,5]
console.log(Array.isArray(arr)); // true
// The Judgment Method of ES3
console.log(typeof arr); // object
console.log(arr.toString()); // 3,4,5
console.log(arr instanceof Array); //true

Summary: Some of the above methods are often needed. Record the data for the convenience of the method work and get the data you want.

Tags: Javascript encoding

Posted on Thu, 08 Aug 2019 20:29:34 -0700 by Papalex606