关于数组删除
Posted lixintao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于数组删除相关的知识,希望对你有一定的参考价值。
let arr1 = [‘one‘, ‘two‘, ‘there‘],
arr2 = [‘red‘, ‘yellow‘, ‘blue‘],
arr3 = [‘brid‘, ‘dog‘, ‘pig‘],
arr4 = [‘orange‘,‘apple‘,‘banner‘],
arr5 = [18,19,20],
arr6 = [‘car‘,‘smallCar‘,‘bus‘],
arr7 = [‘rice‘,‘pizza‘,‘noodle‘]
function fristWay() {
document.getElementsByClassName(‘numbershow‘)[0].getElementsByTagName(‘p‘)[0].innerHTML = "before "+ arr1 + " " + arr1.length;
arr1.length=2;
document.getElementsByClassName(‘numbershow‘)[0].getElementsByTagName(‘span‘)[0].innerHTML = "after " + arr1 + " " + arr1.length;
}
function secondWay() {
document.getElementsByClassName(‘numbershow‘)[0].getElementsByTagName(‘p‘)[0].innerHTML = "before "+ arr2 + " " + arr2.length;
delete arr2[2]
document.getElementsByClassName(‘numbershow‘)[0].getElementsByTagName(‘span‘)[0].innerHTML = "after " + arr2 + " " + arr2.length;
}
function thirdWay() {
document.getElementById(‘annimals‘).getElementsByTagName(‘p‘)[0].innerHTML = "before "+ arr3 + " " + arr3.length;
arr3.pop()
document.getElementById(‘annimals‘).getElementsByTagName(‘span‘)[0].innerHTML = "after "+ arr3 + " " + arr3.length;
}
function fourthWay() {
document.getElementsByClassName(‘fruits‘)[0].getElementsByTagName(‘p‘)[0].innerHTML = "before "+ arr4 + " " + arr4.length;
arr4.shift()
document.getElementsByClassName(‘fruits‘)[0].getElementsByTagName(‘span‘)[0].innerHTML = "after " + arr4 + " " + arr4.length;
}
function fifthWay() {
document.getElementsByClassName(‘age‘)[0].getElementsByTagName(‘p‘)[0].innerHTML = "before "+ arr5 + " " + arr5.length;
arr5.splice(0,2)
document.getElementsByClassName(‘age‘)[0].getElementsByTagName(‘span‘)[0].innerHTML = "after " + arr5 + " " + arr5.length;
}
function sixthWay() {
document.getElementsByClassName(‘transportiaon‘)[0].getElementsByTagName(‘p‘)[0].innerHTML = "before "+ arr6 + " " + arr6.length;
// arr6.forEach(function(item, index, arr) {
// if(item == "smallCar") {
// arr6.splice(index, 1);}
// });
arr6=arr6.filter(function(item) {
return item != "smallCar"
});
document.getElementsByClassName(‘transportiaon‘)[0].getElementsByTagName(‘span‘)[0].innerHTML = "after " + arr6 + " " + arr6.length;
}
以上是关于关于数组删除的主要内容,如果未能解决你的问题,请参考以下文章