js数组精讲和排序算法 mdn
数组和length
const arr = [];
const arr1= new Array();
consr arr = [1,2,3]
arr.length = 2;
console.log(arr)
const [a, ...b, c] = [1,2,3,4,5]
concat
let concat_demo1 = () => {
let a = ['js','css','html'];
let b = ['c','c++'];
let d = ['java','python'];
let c = a.concat(b, d);
c.concat('c#')
console.log(c)
b[0] = 99;
console.log(b)
console.log(c)
}
let concat_demo2 = () => {
let tom = [
{
name: 'tom',
age: 21
}
];
let mary = [
{
name: 'mary',
age: 33
},
];
let c = tom.concat(mary);
console.log(c)
tom[0].name = 'tommy'
console.log(c)
}
自行实现concat
Array.prototype.concat1 = function (...args) {
const res = []
args.forEach((item ,index, arr)=> {
if(Array.isArray(item)){
res.push(...item)
}else {
res.push(item)
}
})
return [...this, ...res]
};
join
const join_demo1 = () => {
const arr = ['吃','喝', '睡', '笑']
const res = arr.join("&")
console.log(res)
}
pop
const pop_demo = () => {
const arr = ['小花','小丽','小红','小明']
const res= arr.pop()
console.log(arr)
console.log(res)
}
push
const push_demo = () => {
const arr = ['大黄']
arr.push('小黑')
arr.push('旺财','大金', '老王')
console.log( arr.push(9))
}
push_demo()
reverse
const arrr = [1,2,3,4]
console.log(arrr.reverse())
shift
const shift_demo = () => {
const arr = ['产品经理','前端开发','后端开发','设计师']
console.log(arr.shift())
}
unshift
const unshift_demo = () => {
const arr = ['产品经理','前端开发','后端开发','设计师']
console.log(arr.unshift('测试'))
arr.unshift('项目经理', '数据')
}
unshift_demo()
slice
const slice_demo = () => {
const arr = ['苹果', '小米' , '三星' ,'华为']
const arr1 = arr.slice(0, 2)
const arr2 = arr.slice(0 ,-1)
const arr3 = arr.slice(-1);
const arr4 = arr.slice(0)
console.log(arr1)
console.log(arr2)
console.log(arr3)
console.log(arr4)
}
splice
const splice_demo = () => {
const arr = ['苹果', '香蕉' , '榴莲' ,'葡萄'];
const item = arr.splice(1, 1);
console.log(item)
console.log(arr)
arr.splice(0,0, '橘子', '荔枝', '哈密瓜')
console.log(arr)
}
includes
const includes_demo = () => {
const arr = [NaN, NaN, 1, 2]
console.log(arr.includes(NaN))
console.log(arr.indexOf(NaN))
}
includes_demo()
forEach,map, filter, some, every, reduce
const loop_demo = () => {
const arr = [11,2,23,4,25];
arr.forEach((item,index,arr) => {
})
const arr2 = arr.map((item,index,arr) => {
return item *2;
})
const arr3 = arr.filter((item,index,arr) => {
return item > 10
})
const arr5 = arr.every(item => item > 10)
console.log(arr5)
const arr4 = arr.some((item,index,arr) => {
return item > 10
})
const list = ['tom', 'lisa', 'tina']
const res = list.reduce((prev, item,index) => {
prev[item] = index;
return prev;
}, {})
console.log(res)
}
const arr1 = [
[11,22],
[77,99],
[88,66]
]
const res = arr1.reduce((prev ,item, index, arr) => (prev['x'+ (index+1)] = item[0], prev['y'+ (index+1)] = item[1], prev) , {})
console.log(res)
loop_demo()
数组排序1 - 默认排序
const list = [3, 999,22, 1, 223, 99, 8, 7, 6, 0];
list.sort(function (a, b) {
return a - b
})
数组排序2 - 选择排序
function getMin(arr) {
const _index = 0;
const min = arr[0];
arr.forEach((item, index) => {
if (item < min) {
min = item;
_index = index;
}
})
return _index;
}
function chooseSort(arr) {
let res = [];
while (arr.length) {
res = [...res, ...arr.splice(getMin(arr), 1)]
}
return res;
}
数组排序3 - 冒泡排序
function bubbleSort(list) {
const {length} = list;
for (let a = 0; a < length -1; a++) {
for(let b = 0; b < length -1-a; b++){
if (list[b] > list[b + 1]) {
const temp = list[b];
list[b] = list[b +1]
list[b +1] = temp;
}
}
}
}