本文参考原文-http://bjbsair.com/2020-03-25/tech-info/6344.html
封装方法传送门:
使用方法非常简单,只需放到你的 utils.js 工具文件中,直接 export const 加上我的封装方法,在别的文件中使用{方法1,方法2,方法3…}引用后就可以直接使用了!
升序 bubbleAsSort()
bubbleAsSort = arr => {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
复制代码
降序 bubbleDeSort()
bubbleDeSort = arr => {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
let temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
复制代码
升序 selectAsSort()
selectAsSort = arr => {
let minIndex, temp;
for (let i = 0; i < arr.length - 1; i++) {
minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
复制代码
降序 selectDeSort()
selectDeSort = arr => {
let minIndex, temp;
for (let i = 0; i < arr.length - 1; i++) {
minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] > arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
复制代码
升序 insertAsSort()
insertAsSort = arr => {
let current, preIndex;
for (let i = 1; i < arr.length; i++) {
current = arr[i];
preIndex = i - 1;
while (preIndex >= 0 && arr[preIndex] > current) {
arr[preIndex + 1] = arr[preIndex];
preIndex--;
}
arr[preIndex + 1] = current;
}
return arr;
}
复制代码
降序 insertDeSort()
insertDeSort = arr => {
let current, preIndex;
for (let i = 1; i < arr.length; i++) {
current = arr[i];
preIndex = i - 1;
while (preIndex >= 0 && arr[preIndex] < current) {
arr[preIndex + 1] = arr[preIndex];
preIndex--;
}
arr[preIndex + 1] = current;
}
return arr;
}
复制代码
arrDemp1 = arr => {
let newArr = [];
let m = {};
for (let i = 0; i < arr.length; i++) {
let n = arr[i];
if (m[n]) {
} else {
newArr.push(arr[i]);
m[n] = true;
}
}
return newArr;
}
//遍历数组法
arrDemp2 = arr => {
let temp = [];
for (let i = 0; i < arr.length; i++) {
//indexOf()方法可返回某个指定的字符串或数组值在字符串或数组中首次出现的位置,若不在其中则返回-1
if (temp.indexOf(arr[i]) === -1)
temp.push(arr[i]);
}
return temp;
}
//排序法
arrDemp3 = arr => {
let temp = [];
arr.sort();
temp.push(arr[0]);
//因为数组已经经过排序,所以重复元素一定相邻,判断当前数组第i个元素与temp的最后一个元素是否相等,不相等时才复制元素
for (let i = 1; i < arr.length; i++) {
if (arr[i] != temp[temp.length - 1])
temp.push(arr[i]);
}
return temp;
}
//对象法
arrDemp4 = arr => {
let temp = [];
let json = {};
//将当前数组的元素值当作对象的属性,遍历数组,比对对象,如果对象的这个属性不存在则将当前数组元素复制到临时数组,并添加该属性且将属性值赋值为1
for (let i = 0; i < arr.length; i++) {
if (!json[arr[i]]) {//如果对象没有该属性
temp.push(arr[i]);
json[arr[i]] = 1;//添加属性,将属性值赋值为1
}
}
return temp;
}
复制代码
也可以使用ES6中的new Set,一步到位
let arr = [1,2,3,5,4,5,4,3,6]
let arrDemp = new Set(arr) //arrDemp是一个对象
let newArr = [...arrDemp] //把arrDemp转化成数组
console.log(newArr);
复制代码
将对象数组中属性相同的项去重
/*
* objArr 对象数组
* para 将要进行去重的字段(String类型)
*/
objArrDemp1 = (objArr, para) => {
let result = [];
let temp = {};
for (let i = 0; i < objArr.length; i++) {
let parameter = objArr[i][para];
if (temp[parameter]) {
continue;//不继续执行接下来的代码,跳转至循环开头
}
temp[parameter] = true;//为temp添加此属性(parameter)且将其值赋为true
result.push(objArr[i]);//将这一项复制到结果数组result中去
}
return result;
}
objArrDemp2 = (objArr, para) => {
let hash = {};
//reduce方法有两个参数,第一个参数是一个callback,用于针对数组项的操作;第二个参数则是传入的初始值,这个初始值用于单个数组项的操作。
objArr = objArr.reduce(function (item, next) {//这是针对数组项操作的函数,对于每个数组项,reduce方法都会将其调用一次
hash[next[para]] ? '' : hash[next[para]] = true && item.push(next);
return item;
}, []);//初始值是一个空对象,使用reduce方法返回的是空对象通过叠加执行之后的结果
return objArr;
}
// 测试数据:
let objArr = [{ name: 'a', age: 1 }, { name: 'a', age: 2 }, { name: 'b', age: 2 }]
console.log(objArrDemp1(objArr,'name')); // [ { name: 'a', age: 1 }, { name: 'b', age: 2 } ]
console.log(objArrDemp1(objArr,'age')); // [ { name: 'a', age: 1 }, { name: 'a', age: 2 } ]
复制代码
staArrNum = arr => {
let obj = {};
for (let i = 0; i < arr.length; i++) {
let m = arr[i];
if (obj.hasOwnProperty(m)) {
obj[m] += 1;
} else {
obj[m] = 1;
}
}
return obj;
}
// 测试数据
let arr = [1, 2, 3, 6, 5, 3, 2, 1, 2, 3, 2, 1]
console.log(staArrNum(arr)); // { '1': 3, '2': 4, '3': 3, '5': 1, '6': 1 }
复制代码
arrFinNum = function (arr,num) {
let index = -1;
for (let i = 0; i < arr.length; i++) {
if (num == arr[i]) {
index = i;
break;
}
}
return index;
}
// 测试数据
let arr = [1,2,3,4,5,6]
console.log(arrFinNum(arr,4)); // 3
复制代码
delArrNum = (arr,val) => {
let index = arrFinNum(arr, val) //调用了前面自行添加的arrFinNum方法
if (index != -1) {
return arr.splice(index, 1);
}
}
复制代码
示例
arrFinNum = (arr, num) => {
let index = -1;
for (let i = 0; i < arr.length; i++) {
if (num == arr[i]) {
index = i;
break;
}
}
return index;
}
delArrNum = (arr,val) => {
let index = arrFinNum(arr, val) //调用了前面自行添加的arrFinNum方法
if (index != -1) {
return arr.splice(index, 1);
}
}
// 测试数据
let arr = [1, 2, 3, 4, 5, 6]
console.log(delArrNum(arr,2)); // [ 2 ]
复制代码
//非递归实现
binarySearch = (arr, key) => {
let high = arr.length - 1,
low = 0;
while (low <= high) {
let m = Math.floor((high + low) / 2);
if (arr[m] == key) {
return m;
}
if (key > arr[m]) {
low = m + 1;
} else {
high = m - 1;
}
}
return false;
}
// 测试数据
let arr = [-1, 1, 3, 4, 5, 8, 32, 234, 12, 42];
console.log(binarySearch(arr, 4));
复制代码
//递归实现
binarySearch = (arr, low, high, key) => {
if (low > high) {
return -1;
}
let mid = parseInt((high + low) / 2);
if (arr[mid] == key) {
return mid;
} else if (arr[mid] > key) {
high = mid - 1;
return binarySearch(arr, low, high, key);
} else if (arr[mid] < key) {
low = mid + 1;
return binarySearch(arr, low, high, key);
}
};
// 测试数据
let arr = [-1, 1, 3, 4, 5, 8, 32, 234, 12, 42];
console.log(binarySearch(arr, 0, 13, 5));
复制代码
/**
* obj 需要处理的对象
*/
objToArrObj = obj => {
let arr = []
for(let i in obj){
arr.push({[i]:obj[i]})
}
return arr
}
// 测试数据
let obj = {20180410: 5, 20180411: 13, 20180412: 26, 20180413: 16}
console.log(objToArrObj(obj));
/*
[
{ 20180410: 5 },
{ 20180411: 13 },
{ 20180412: 26 },
{ 20180413: 16 }
]
*/
复制代码
/**
* arr 对象数组
* index 要查找的键名
*/
objArrHandle = (arr,keyName) => {
let sub = arr.findIndex(item=>item[keyName])
let obj = arr[sub]
let key = Object.keys(obj)[0]
let value = obj[Object.keys(obj)]
return '下标:'+sub+' 键:'+key+' 值:'+value
}
// 测试数据
let arr = [{ 20180410: 5 },{ 20180411: 13 },{ 20180412: 26 },{ 20180413: 16 }]
console.log(objArrHandle(arr,20180412)); // 下标:2 键:20180412 值:26