5 个JavaScript 中 Slice()的用例
web前端开发
共 1988字,需浏览 4分钟
·
2021-11-27 10:44
![](https://filescdn.proginn.com/a53bc33d52b476bf6b57e59a0de7caec/a19a30081e653aa6d06c32e11a195d64.webp)
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/4e9153e1726fa49078611ac989d4a5d0/990f5f1e7574231cc7f5f1bed11815c7.webp)
![](https://filescdn.proginn.com/4e9153e1726fa49078611ac989d4a5d0/990f5f1e7574231cc7f5f1bed11815c7.webp)
![](https://filescdn.proginn.com/997ae9be172248c4b8dfe88dcbb8e0ad/d0336cff1b9295cd12723a1cc7b94b56.webp)
评论