数组相关(常用数组方法)
- find
- findIndex
- forEach
- map
- pop
- reduce
- push
- some
- every
- includes
- join
- sort
- splice
- filter
- reverse
- indexOf
let tempArr = [1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 0],
tempObjArr = [
{ a: 1, b: 3 },
{ a: 2, b: 3 },
{ a: 3, b: 3 },
{ a: 4, b: 3 },
];
let tmpfind = tempArr.find(item => item === 1);
console.log('tmpfind:', tmpfind)
let idx = tempArr.findIndex(item => item === 1);
console.log(idx)
tempArr.forEach((item, index) => {
if (item === 3) {
tempArr[index] = 13
}
});
console.log(tempArr);
let mapArr = tempObjArr.map(item => ({ c: item.a, d: item.b }));
console.log('mapArr:', mapArr);
tempArr.pop(tempArr);
console.log('pop', tempArr);
tempArr.push(14);
console.log(tempArr);
let reduce1 = tempArr.reduce((a, b) => {
if (b <= 4) {
a.push(b);
}
return a;
}, []);
let reduce2 = tempArr.reduce((a, b) => a + b);
console.log('reduce1:', reduce1, 'reduce2:', reduce2);
tempArr.shift();
console.log('shift:', tempArr);
let bSome = tempArr.some(item => item > 6);
if (bSome) {
console.log('someTrue:', bSome)
} else {
console.log('someFalse:', bSome)
}
let bEvery = tempArr.every(item => item > 0);
if (bEvery) {
console.log('everyTrue', bEvery);
} else {
console.log('everyFalse', bEvery);
}
let bIncludes = tempArr.includes(2);
console.log('bIncludes:', bIncludes);
let joinStr = tempArr.join(':');
console.log('joinStr', joinStr);
tempArr.sort((a, b) => b - a);
console.log('sort:', tempArr);
tempArr.splice(0, 1);
console.log('splice:', tempArr);
let filterArr = tempArr.filter(item => item >= 13);
console.log('filterArr:', filterArr);
let indexOf = tempArr.indexOf(2);
console.log('indexOf:', indexOf);
tempArr.reverse();
console.log('reverse:', tempArr);