js字符串,数组操作大全

let str = 'hello world';
let str1 = "hello";
let str2 = "hello1";
let str3 = 'hello WORLD';
let str4 = ' hello world ';

//charAt(index) 将字符串视为字符数组。它检索你提供的索引处的字符
console.log(str.charAt(0),str.charAt(1),str.charAt(2)); // 'h' 'e' 'l'

//includes(string) 检查字符串是否包含子字符串
console.log(str.includes('hello'), str.includes('word')); // true false

//match(regex string) 字符串是否与正则表达式匹配
const nameRegex = /^[a-zA-Z]+$/
console.log(str1.match(nameRegex), str2.match(nameRegex)); // true false

//replace(str,str1) 获取字符串中出现的字符并将其替换为另一个字符
console.log(str.replace('l','L'), str.replace(/l/g,"L")); // 'heLlo world''heLLo worLd'

//split(string) 根据某个字符或其他字符串拆分字符串
console.log(str.split(' ')); // ['hello', 'world']

//substring(index, index) 索引范围拆分字符串
console.log(str.substring(1, 4), str); // 'ell' 'hello world'

//substr(n,m) 从索引n开始,截取m个字符
console.log(str.substr(1, 4), str) // 'ello' 'hello world'

//slice(n,m) 从索引n开始,截取到索引m,不包括m
console.log(str.slice(1, 4), str) // 'ell' 'hello world'

//toLowerCase()/toUpperCase() 将字符串转换为全部大写或全部小写
console.log(str3.toUpperCase(),str3.toLowerCase()); //'HELLO WORLD' 'hello world'

//trim() 从字符串中删除头尾空格
console.log(str4.trim()) // 'hello world'

//indexOf()/lastIndexOf()查询元素位置
console.log(str.indexOf('o'), str.lastIndexOf('o'))  //  4 7


//push 向数组尾部添加元素
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.push(8,9);
console.log(a,array); //9 [1,2,3,4,5,6,7,8,9]

//unshift 向数组头部添加元素
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.unshift(8,9);
console.log(a,array); // 9 [8,9,1,2,3,4,5,6,7]

//pop 从数组尾部删除一个元素
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.pop();
console.log(a,array); // 7 [1,2,3,4,5,6]

//shift 从数组头部删除一个元素
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.shift();
console.log(a,array); // 1 [2,3,4,5,6,7]

//slice 不改变原数组的截取
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.slice(1,3);
console.log(a, array); // [2,3] [1,2,3,4,5,6,7]

//splice 改变原数组的截取
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.splice(1,3);
console.log(a, array); // [2,3,4] [1,5,6,7]

//reverse 数组倒序
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.reverse()); // [7,6,5,4,3,2,1]

//sort 数组排序
let array = [1, 2, 3, 4, 5, 6, 7];
array.sort((a,b)=>{
    return b - a; // 从大到小  return a - b;从小到大
})
console.log(array); // [7,6,5,4,3,2,1]

//join 数组按照指定分隔符转换成字符串
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.join('-')); // '1-2-3-4-5-6-7'

//concat 数组拼接
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.concat([8,9])); // [1,2,3,4,5,6,7,8,9]

//indexOf/lastIndexOf查询元素位置
let array = [1, 2, 3, 4, 5, 6, 7,1];
console.log(array.indexOf(1), array.lastIndexOf(1))  //  0 7

//Array.from 将类数组对象或可迭代对象转化为数组,比如:arguments,js选择器找到的dom集合
<div class="number">1</div>
<div class="number">2</div>
let domArray = document.getElementsByClassName('number')
console.log(domArray, Array.from(domArray)) 
//HTMLCollection(2)[div.number, div.number] [div.number, div.number]

//includes(value: 搜索值,stratIndex:搜索的起始位置) 检测数组中是否包含一个值
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.includes(1), array.includes(1,1)); //true false

//fill(value,startIndex,endIndex) 将一定范围索引的数组元素内容填充为单个指定的值
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.fill(0,1,2)); // [1,0,3,4,5,6,7]

//flat(num: 嵌套层数) 嵌套数组转一维数组
let array = [1, [2, [3, ,4], 5], 6, 7];
console.log(array.flat()); //[1,2,[3,4],5,6,7]
console.log(array.flat(2)); //[1, 2, 3, 4, 5, 6, 7]
console.log(array.flat(Infinity)); //[1, 2, 3, 4, 5, 6, 7] Infinity不管有多少层执行到底

拓展:

1.数组去重,取重
let array = [1,1,2,2,3,3,4,5,6]
//去重
let newArray = [...new Set(array)]
//取重
let compArray = array.filter((el,index)=>
   ((array.indexOf(el) != array.lastIndexOf(el)) && (array.indexOf(el) == index))
)
console.log(newArray,compArray)// [1, 2, 3, 4, 5, 6]  [1, 2, 3]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王氏gai

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

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

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

打赏作者

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

抵扣说明:

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

余额充值