数组修改数据
数组.splice(参数1,参数2,参数3…参数n)
splice会修改原数组
1个参数:从这个参数的下标截取到最后一个值,存储为一个新数组返回
2个参数:第1个参数:起始下标第2个参数:截取的个数
3个下标:第1个参数是起始下标,第2个参数是截取的个数(0代表插入,非0表示修改),第3个参数及第3个参数以后的内容,都是插入或修改的值
let aliang01 = ["A","B","C","E","F"];
let aliang02 = aliang01.splice(2)
console.log(aliang01);
console.log(aliang02);
let aliang03 = ["A","B","C","E","F"];
let aliang04 = aliang03.splice(0,3)
console.log(aliang03);
console.log(aliang04);
let aliang05 = ["A","B","C","D","E","F"];
aliang06返回的[ 'C', 'D', 'E' ]
let aliang08 = aliang05.splice(2,3,3,'o','6',5,8);
console.log(aliang05);
console.log(aliang08);
冒泡排序法
冒泡排序法(升序与降序):
相邻的两个值进行比较,满足条件就互换位置,不满足就不换位置
let num1 = [3, 65, 54, 21, 9, 71, 23, 8, 11];
let num2 = [62,4,798,13,15,18,30,22,41];
for (let i = 0; i < num1.length; i++) {
for (let j = 0; j < num1.length&&j < num2.length ; j++) {
if (num1[j] > num1[j + 1]) {
[num1[j], num1[j + 1]] = [num1[j + 1], num1[j]]
} ;
if (num2[j] < num2[j + 1]) {
[num2[j], num2[j + 1]] = [num2[j + 1], num2[j]]
};
}
}
console.log(num1, num2);
选择排序法:
记录满足条件的最大或最小值的下标并进行互换
内层循环执行一次,就会得到最小或最大值
let num1 = [3, 65, 54, 21, 9, 71, 23, 8, 11];
let num2 = [62,4,798,13,15,18,30,22,41];
for (let i = 0; i < num1.length-1&&i < num2.length-1; i++) {
for (let j = i+1; j < num1.length&&j < num2.length; j++) {
if (num1[i] < num1[j]) {
[num1[i], num1[j]] = [num1[j], num1[i]]
}
if (num2[i] > num2[j]) {
[num2[i], num2[j]] = [num2[j], num2[i]]
};
}
}
console.log(num1,num2);
即,每次内循环比较后,都有一个最大值或最小值,所以下一次内循环比较就可以不用从之前比较过的已确定最大或最小的值开始进行比较,即外层循环减一