javascript中最常用的数组,字符串方法


一、数组方法


1、创建数组

var arr= new Array()

2、检测变量是否是数组

(1)instanceof:

作用:检测变量是否是某个构造函数的实例

语法:变量 instanceof 构造函数名

返回值:true || false;

(2)Array.isArray(变量):

返回值: true || false

例子:

var age = 100;

var arr = ['red','green','blue'];

console.log(age instanceof Array); // false

console.log(Array.isArray([1,2,3])); // true

3、添加数组元素

(1)末尾添加--push

语法: 数组.push(新元素1,新元素2, ...)

返回值:数组最新的长度

(2)前面添加--unshift

语法: 数组.unshfit(新元素1,新元素2, ...)

返回值:数组最新的长度

4、删除数组元素

(1)删除最后一个元素--pop

语法: 数组.pop();

返回值:被删除的元素

(2)删除第一个元素--shift

语法: 数组.shift()

返回值:被删除的元素

5、翻转数组--reverse

语法: 数组.reverse()

返回值:翻转后的数组

6、数组元素排序

(1)从小到大排

数组.sort(function(a,b){

return a-b;

})

(2)从大到小排

数组.sort(function(a,b){

return b-a;

})

7、查找元素位置

(1)查找第一次出现的位置

语法: 数组.indexof('给定的元素')

返回值:元素的索引(数组中没有该元素,则返回-1)

(2)查找最后一次出现的位置

语法: 数组.lastIndexof('给定的元素')

返回值:元素的索引(数组中没有该元素,则返回-1)

8、数组转为字符串

(1)数组.toString()

(2)数组.join('分隔符')

9、数组合并concat

语法:数组.concat("aa")

aa可以为:数组,字符串

数组.concat(元素1,元素2)

数组.concat(数组1,数组2)

返回值:返回合并后的新数组

10、删除插入替换splice

语法:数组.splice(开始位置,删除的个数,插入的元素)

返回值:删除的元素(数组形式返回)

11、拼接数组

const array1 = ['a', 'b', 'c']; ​ const array2 = ['d', 'e', 'f']; ​ const array3 = array1.concat(array2);

console.log(array3); ​ // expected output: Array ["a", "b", "c", "d", "e", "f"]

12、array.every();检查数组中的每一个元素是否能通过某个指定的函数的测试 返回一个布尔值

            const isBelowThreshold = (currentValue) => currentValue < 40;

            const array1 = [1, 30, 39, 29, 10, 13];

            console.log(array1.every(isBelowThreshold));

            // expected output: true

13、arry.fillter() 返回一个新数组包含的是通过所提供得函数测试得元素

          const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

          const result = words.filter(word => word.length > 6);

        console.log(result);   

14、array.find() 返回数组得中适合回调函数中得条件得第一个值 一般得配合循环

                    const array1 = [5, 12, 8, 130, 44];

                    const found = array1.find(element => element > 10);

15、array.foreach(function(value,item){ })有三个参数最后一个参数可以改变this指向

                    const array1 = ['a', 'b', 'c'];

                    array1.forEach(element => console.log(element));   

17、array.includes( )判断数组是否包含一个指定的值 返回为布尔值

array.idnexof( )返回指定元素得下标 不存在则返回 -1

const array1 = [1, 2, 3];

console.log(array1.includes(2)); ​ // expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat')); ​ // expected output: true

console.log(pets.includes('at')); ​ // expected output: false

18、array.map() 将所有元素执行回调函数返回一个新的数组 语法:

              //例子1

                    const array1 = [1, 4, 9, 16];

                    // pass a function to map

                    const map1 = array1.map(x => x * 2);

                    console.log(map1);

                    // expected output: Array [2, 8, 18, 32]

19、arrray.slice()浅拷贝 返回新的数组 不改变原数组

slice() 方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的 浅拷贝(包括 begin,不包括end)。原始数组不会被改变。

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2)); // expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4)); // expected output: Array ["camel", "duck"]

20、array.some( function( element,[,index [ ,array] ]){ },thisarg )返回值数组中至少有一个通过就是true

const array = [1, 2, 3, 4, 5];

// checks whether an element is even

const even = (element) => element % 2 === 0;

console.log(array.some(even));

// expected output: true

二、字符串方法


1、字符串长度 length

var zifu ="xxxx"

var len= zifu.length

2、遍历字符串

for(var i=0;i<str.length;i++){

    console.log(str[i]);

}

3、查找字符出现的位置

(1)indexof:只会找出第一次出现的下标

语法:字符串.indexof('给定的字符',开始查找的位置)

返回值:返回该字符串的索引值

(2)lastIndexOf则会找出最后一次出现

语法:字符串.lastIndexof('给定的字符',开始查找的位置)

返回值:返回该字符串的索引值

4、根据位置返回字符

字符串.charAt(下标):返回字符

字符串.charCodeAt(索引号):返回字符对应的ASCII码

5、合并字符串

语法:字符串.concat('拼接的字符串');

6、截取字符串

语法:字符串.substr(开始位置,截取长度)

返回值:截取到的新字符串

7、替换字符串

语法:字符串.replace('要替换的字符','替换成的字符')

作用:把字符串第一个指定的字符替换成新字符

返回值:返回替换后的新字符串

8、字符串转数组

语法:字符串.split('分隔符');

要和数组转为字符串区分开:

数组为toString()和join()

9、大小写转换

(1)转为大写

字符串.toUpperCase()

(2)转为小写

字符串.toLowerCase()

10、判断字符串里面是否含有:string.includes( )

var str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true

console.log(str.includes('question'));    // true

11、str.indexOf() 

方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。

12、string.replace(old,new)

        把old替换为new

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西瓜不爱梅汁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值