js common method


1.charAt (); Returns the specified character position, the index ranges from 0 to length () - 1.

var str="abcdefghijklmn"
var str2 = str.charAt(2);
var str3 = str.charAt(8);
console.log(str2); //c
console.log(str3); //i

 2.charCodeAt (); Unicode encoding returns the specified character position

STR = var "Lily NiHao"; 
var str2 = str.charCodeAt (5); // character position 5 is h, h is a Unicode encoding 104 
var Str3 str.charCodeAt = (. 3); 
the console.log (Str3); 121 // 
the console.log (str2); // 110

 3.conca (); connection string

str1 = Ar "hellow"; 
var str2 = 'World'; 
the console.log (str2.concat (str1)); // connected to the str1 str2

 4.indexOf (); search character string, returns a character string in the index

str1 = var "hellow"; 
var num1 = str1.indexOf ( 'O'); // search character string, returns a character string in the index 
// index string is counted from 0 
var num2 = str1.indexOf ( 'lllll'); // no value match is found, return -1 
var num3 = str1.indexOf ( 'Ni'); // matching value is not found, return -1 
the console.log (num1); . 4 // 
the console.log (num2); // -. 1 
the console.log (num3); // -. 1

 5.match (); retrieving a value specified in string or to find one or more regular expression matching, the return value is not the value of the position.

str1 = var "hellow"; 
var str2 = str1.match ( 'ELL'); // search character string, returns a character 
var str3 = str1.match ( 'lll' ); // not specified character it is null 
the console.log (str2); 
the console.log (Str3);

 

 

 6.replace (); replace the matching string

str1 = var "nihaoma"; 
var str2 = "henhao"; 
var = Str3 str1.replace (str1.match ( 'Hao'), str2.match ( 'HEN')); // find hao str1 to find the str2 lixi, replaced with off str2 str1 
the console.log (Str3); // nihenma

 7.search (); retrieving substring of string matching, the return address

str1 = var "Lily nihaoya zaiganma"; 
var str2 = "nihaoya"; 
var Str3 = "wobuzai"; 
var num1 = str1.search (str2); // returns the position of the first character where 
var num2 = str1. search (str3); // not detected return -1 
the console.log (num1); //. 9 
the console.log (num2); // -. 1

 8.slice (); extraction string segment, and the return portion is extracted in a new string

str1 = var 'nihaoya wobuhaoya' 
the console.log (str1.slice (4,9)); // start position. 4, into a front end position 9 
console.log (str1.slice (2,14)); // start start position, a front end position end 
console.log (str1); // change the original string 
console.log (str1.slice (-1,0)); // returns the empty string 
console.log ( str1.slice (30,100)); // length exceeds the length of the string is an empty string 
console.log (str1.slice (-1,10)); // returns the empty string

 

9.split (); into the character array

str1 = var "Dan-Sheng-Kuai-Le"; 
the console.log (str1.split ( '')); 
var str2 = str1.split ( '-'); 
the console.log (str2); 
the console.log (str1 ); // original string unchanged

 

 10.toLowerCase (); toLocaleLowerCase (); lowercase string into the

var str = "XIN NAIN HAO ya";
console.log(str.toLocaleLowerCase());
var str1 = str.toLocaleLowerCase();
console.log(str1);
console.log(str);//原字符串不变
console.log('-----------');
console.log(str.toLowerCase());

 

 

 

 11.toUpperCase (); toLocaleUpperCase (); quasi-string into uppercase

var str = "xin nian hao ya";
console.log(str.toLocaleUpperCase());
var str1 = str.toLocaleUpperCase();
console.log(str1);
console.log(str);//原字符串不变
console.log('-----------');
console.log(str.toUpperCase());

 

 

 

12.substr (); bibliographic specified character from the starting character string extraction index

STR = var "Tian Jin Yi Shi 2019 Nian ZUI HOU Tian" 
var = Str3 str.substr (3,19); // string does not occupy the space position 3 from the beginning to the end 19 
console.log (str3);

 

 

 13.subString();提取字符串中两个指定索引号之间的字符

var str = "jin tian shi 2019 nian zui hou yi tian"
var str3 = str.substring(3,19);//字符串中空格占位置,从3开始,到18结束
var str4 = str.substr(3,19);//空格不占位。从3开始到19结束
console.log(str4);
console.log('-------------');
console.log(str3);

 

array数组方法

1.slice[start,end];返回从原数组中指定开始下标到结束下标之间的项组成的新数组(原数组不变)

  • 1个参数:n,即n到末尾的所有
  • 2个参数:[start,end]
var arr = [1,2,3,4,5,6,7,8];
var arr1 = arr.slice(2,6);
console.log(arr1);
//数组下表从0开始,从2开始,到6的前一个位置结束
var arr3 = arr.slice(4); //从4开始到结束
console.log(arr3);

 

2.splice();方法向/从数组中添加/删除项目,然后返回被删除的项目。这种方法会改变原始数组。

arrayObject.splice(index,howmany,item1,.....,itemX)

删除从坐标2开始的3个数

 

删除从坐标2开始的3个数并且6 .7 8 替换

3.pop();方法用于删除数组的最后一个元素并返回删除的元素。此方法改变数组的长度!

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

 

 4.push();向数组的末尾添加一个或多个元素,并返回新的长度。

var arr = [1,2,3,4,5];
console.log(arr.length);
console.log('-----------');
var num = arr.push(6,7,8); 
console.log(num); //返回的是新数组的长度
console.log(arr); //原数组被改变成新数组

 

5.shift();把数组的第一个元素从其中删除,并返回第一个元素的值。

var arr = [1,2,3,4,5];
console.log(arr.length);
console.log('-----------');
var arr1 = arr.shift(); 
console.log(arr1); //返回的是删除的数组的值
console.log(arr); //原数组被改变成新数组

 

6.unshift();向数组的开头添加一个或更多元素,并返回新的长度。

var arr = [1,2,3,4,5];
console.log(arr.length);
console.log('-----------');
var arr1 = arr.unshift(2,4,5,{name:'liqi'}); 
console.log(arr1); //返回的是新数组的长度
console.log('------------');
console.log(arr); //原数组被改变成新数组

 

 7.sort();对数组的元素进行排序。排序顺序可以是字母或数字,并按升序或降序。默认排序顺序为按字母升序。

var points = [40,100,1,5,25,10]; points.sort(function(a,b){return a-b}); //升序
//输出 1,5,10,25,40,100
var points = [40,100,1,5,25,10]; points.sort(function(a,b){return b-a}); //降序
//输出 100,40,25,10,5,1
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort();
//["Apple", "Banana", "Mango", "Orange"]

8.concat(3,4);连接两个或多个数组。该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。

 9.join();方法用于把数组中的所有元素转换一个字符串。元素是通过指定的分隔符进行分隔的。

 

10.indexOf();方法可返回数组中某个指定的元素位置。

该方法将从头到尾地检索数组,看它是否含有对应的元素。开始检索的位置在数组 start 处或数组的开头(没有指定 start 参数时)。如果找到一个 item,则返回 item 的第一次出现的位置。开始位置的索引为 0。

如果在数组中没找到指定元素则返回 -1。

 

var arr = [1,2,3,4,5,6,78];
var num = arr.indexOf(78,3);//查找78所在的位置
console.log(num); //返回的项的索引
var num1 = arr.indexOf(3,1);
console.log(num1);
var num2 = arr.indexOf(2);
console.log(num2);
console.log(arr);

 

11.reverse() ;方法用于颠倒数组中元素的顺序。

 

 

 12.foeEach();forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数

  注意: forEach() 对于空数组是不会执行回调函数的

13.map();方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

  注意: map() 不会对空数组进行检测,不会改变原始数组。

 

 

 14.isArray() 方法用于判断一个对象是否为数组。如果对象是数组返回 true,否则返回 false。

 

 

 

 15.every();用于检测数组所有元素是否都符合指定条件

  • 数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
  • 如果所有元素都满足条件,则返回 true。
var arr = [1,6,8,-2,-5,7,-4]
var isPositive = arr.every(function(value){
    return value > 0;
})
console.log(isPositive) // false

 16.some() 方法用于检测数组中的元素是否满足指定条件(函数提供)。

  some() 方法会依次执行数组的每个元素:

  • 如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。
  • 如果没有满足条件的元素,则返回false。
var arr = [1,6,8,-2,-5,7,-4]
var isPositive = arr.some(function(value){
    return value > 0;
})
console.log(isPositive) // true

 17.filter(); 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

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

 

Guess you like

Origin www.cnblogs.com/Kyaya/p/12093018.html