5 个JavaScript 中 Slice()的用例
前端Q
共 4106字,需浏览 9分钟
·
2021-11-28 19:02
点击上方 前端Q,关注公众号
回复加群,加入前端Q技术交流群
y = [1, 2, 3, 4, 5, 6]
y.slice(2, -2) // will return [3, 4]
const midtermGrades = updatedGrades.slice();
const successfulStudents = allStudents.slice(10);
const unsatisfactoryStudents = allStudents.slice(-10);
function TransformToArray(){
return Array.prototype.slice.call(arguments);
}
var newArray = TransformToArray("1", "2", "3", "4");
console.log(newArray); // ["1", "2", "3", "4"];
var p = document.querySelectorAll(‘p’);
var pNodes = Array.prototype.slice.call(p);
String.prototype.append = function (index,value) {
return this.slice(0,index) + value + this.slice(index);
};
var s = "Happy year";
alert(s.append(6,"new "));
复制数组
构造一个从 n 开始的子数组
将类数组对象转换为数组
将 NodeList 转换为数组
替换字符串中的特定索引
![](https://filescdn.proginn.com/92225888e544c405909af1ef541949fe/edb05cbaf695354738710930c5eea1f0.webp)
往期推荐
![](https://filescdn.proginn.com/28e7d4fabe1971a2b201a6545d962152/483382aea66ad6dcea176dd1ca3cd5d6.webp)
![](https://filescdn.proginn.com/70464c1df1f1c25b684a59c66a15a49d/62eac463a114c0fc4125634994617b5d.webp)
![](https://filescdn.proginn.com/70464c1df1f1c25b684a59c66a15a49d/62eac463a114c0fc4125634994617b5d.webp)
最后
欢迎加我微信,拉你进技术群,长期交流学习...
欢迎关注「前端Q」,认真学前端,做个专业的技术人...
![](https://filescdn.proginn.com/9f170b656bf5ac675fbd5de89b2e37bd/dddd9f07b7441e7f0a2b7820acee0cb7.webp)
![](https://filescdn.proginn.com/5377f4f141b9d07bf2521c0688bc4516/7eafa3073c5cd70b8a359573cfaa0f63.webp)
评论