|
|
@@ -2,6 +2,8 @@
|
|
|
//Array对象 Object.defineProperty 增加额外的函数方法
|
|
|
// 在app.js 中增加require即可
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
+const proObj = require('./prototypeObject')
|
|
|
+
|
|
|
// Array.prototype.at函数
|
|
|
if (!Array.prototype.at) {
|
|
|
const atFunc = function (idx) {
|
|
|
@@ -21,18 +23,6 @@ if (!Array.prototype.at) {
|
|
|
Array.prototype.at = atFunc;
|
|
|
}
|
|
|
}
|
|
|
-/**
|
|
|
- * 取最大值
|
|
|
- */
|
|
|
-Array.prototype.getMax = function () {
|
|
|
- var max = this[0];
|
|
|
- for (var index = 1; index < this.length; index++) {
|
|
|
- if (this[index] > max) {
|
|
|
- max = this[index];
|
|
|
- }
|
|
|
- }
|
|
|
- return max;
|
|
|
-}
|
|
|
|
|
|
/**
|
|
|
* 将一个数组元素清空
|
|
|
@@ -63,86 +53,42 @@ Array.prototype.first = function () {
|
|
|
Array.prototype.last = function () {
|
|
|
return this[this.length - 1]
|
|
|
}
|
|
|
-function cacl(arr, callback) {
|
|
|
- // 变量的初始化(治理在使用的时候进行初始化)
|
|
|
- var ret;
|
|
|
- for (var i = 0, len = arr.length; i < len; i++) {
|
|
|
- ret = callback(arr[i], ret);
|
|
|
- }
|
|
|
- return ret;
|
|
|
-}
|
|
|
|
|
|
/**
|
|
|
* 对数组的所有元素进行求和
|
|
|
* @return {*}
|
|
|
*/
|
|
|
-Array.prototype.sum = function () {
|
|
|
- // 1. 一般的方法
|
|
|
- /*var ret = 0;
|
|
|
- for (var i = 0, len = this.length; i < len; i++){
|
|
|
- ret = ret + this[i];
|
|
|
+Array.prototype.sum = function (name) {
|
|
|
+ if (!name) {
|
|
|
+ return this.reduce((acc, cur) => (parseFloat(cur) + acc), 0);
|
|
|
+ } else {
|
|
|
+ return this.filter(it => it[name]).map(it => it[name]).reduce(
|
|
|
+ (acc, cur) => (parseFloat(cur) + acc), 0);
|
|
|
}
|
|
|
- return ret;*/
|
|
|
-
|
|
|
- // 2.使用上面的计算类
|
|
|
- /**
|
|
|
- * @param:item 数组的每一项
|
|
|
- * @param:sum 数组求和的结果
|
|
|
- */
|
|
|
- return cacl(this, function (item, sum) {
|
|
|
- // 如果刚开始没有初始化的话,就直接使用第一项作为sum(ret)的初始值
|
|
|
- if (typeof sum === 'undefined') {
|
|
|
- return item;
|
|
|
- } else {
|
|
|
- return sum += item;
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
}
|
|
|
/**
|
|
|
* 找出数组中的最大值
|
|
|
* @return {*}
|
|
|
*/
|
|
|
-Array.prototype.max = function () {
|
|
|
- // 1. 一般的方式求出最大值
|
|
|
- /*var ret = 0;
|
|
|
- for (var i = 0, len = this.length; i < len; i++){
|
|
|
- if (ret < this[i]){
|
|
|
- ret = this[i];
|
|
|
- }
|
|
|
+Array.prototype.max = function (name) {
|
|
|
+ if (!name) {
|
|
|
+ return this.reduce((acc, cur) => acc > cur ? acc : cur);
|
|
|
+ } else {
|
|
|
+ return this.filter(it => it[name]).map(it => it[name]).reduce(
|
|
|
+ (acc, cur) => acc > cur ? acc : cur);
|
|
|
}
|
|
|
- return ret;*/
|
|
|
-
|
|
|
- // 2. 第二种方式
|
|
|
- return cacl(this, function (item, max) {
|
|
|
- if (typeof max === 'undefined') {
|
|
|
- return item;
|
|
|
- } else {
|
|
|
- if (max < item) {
|
|
|
- return item;
|
|
|
- } else {
|
|
|
- return max;
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
}
|
|
|
/**
|
|
|
* 找出一个数组中的最小值
|
|
|
* @return {*}
|
|
|
*/
|
|
|
-Array.prototype.min = function () {
|
|
|
- return cacl(this, function (item, min) {
|
|
|
- if (typeof min === 'undefined') {
|
|
|
- return item;
|
|
|
- } else {
|
|
|
- // 只要每一项的值都不比最小值小的话
|
|
|
- if (!(min < item)) {
|
|
|
- return item;
|
|
|
- } else {
|
|
|
- return min;
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
+Array.prototype.min = function (name) {
|
|
|
+ if (!name) {
|
|
|
+ return this.reduce((acc, cur) => acc < cur ? acc : cur);
|
|
|
+ } else {
|
|
|
+ return this.filter(it => it[name]).map(it => it[name]).reduce(
|
|
|
+ (acc, cur) => acc < cur ? acc : cur);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -170,48 +116,36 @@ Array.prototype.avg = function () {
|
|
|
}
|
|
|
|
|
|
|
|
|
-// 去除数组中的重复项
|
|
|
-/*
|
|
|
-* 实现思路: 遍历原始数组中的每一项元素,让每次遍历的这一个元素和后面的每一个元素进行比较
|
|
|
-* 【只要相同的话就直接跳过继续向下寻找】
|
|
|
-* */
|
|
|
+/**
|
|
|
+ * @desc : 数组去重
|
|
|
+ * @author : 周兴
|
|
|
+ * @date : 2022/12/14 16:14
|
|
|
+ */
|
|
|
Array.prototype.unique = function () {
|
|
|
- var a = [],
|
|
|
- len = this.length;
|
|
|
- for (var i = 0; i < len; i++) {
|
|
|
- for (var j = i + 1; j < len; j++) {
|
|
|
- if (this[i] === this[j]) {
|
|
|
- // 如果找到了相邻的两个元素是相同的,i直接向后移动一位
|
|
|
- // 然后j开始从i的位置继续向后寻找元素
|
|
|
- j = ++i;
|
|
|
- }
|
|
|
- }
|
|
|
- a.push(this[i]);
|
|
|
+ if (!this || this.length == 0) return this;
|
|
|
+ console.log('ttt',typeof this[0],typeof this[0] === 'object');
|
|
|
+ if (typeof this[0] === 'object') {
|
|
|
+ let arr = []
|
|
|
+ return this.filter(item => !arr.has(item) && arr.push(item))
|
|
|
+ } else {
|
|
|
+ return Array.from(new Set(this));
|
|
|
}
|
|
|
- ;
|
|
|
- return a;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * 去除数组中的重复项
|
|
|
- * 【实现思路】:先对数组进行排序,然后比较相邻的元素是否相同
|
|
|
- * @return {Array}
|
|
|
+ * @desc : 判断数组中是否包含这个对象
|
|
|
+ * @author : 周兴
|
|
|
+ * @date : 2022/11/16 9:11
|
|
|
*/
|
|
|
-Array.prototype.unique = function () {
|
|
|
- var tmp = [],
|
|
|
- len = this.length;
|
|
|
- // 1.先对原始的数组进行排序
|
|
|
- this.sort();
|
|
|
- // 2.比较相邻的元素
|
|
|
- for (var i = 0; i < len; i++) {
|
|
|
- // 只要相邻的元素相同,就直接跳过
|
|
|
- if (this[i] === this[i + 1]) {
|
|
|
- continue;
|
|
|
+Array.prototype.has = function (obj) {
|
|
|
+ if (this && this.length > 0) {
|
|
|
+ for (let i of this) {
|
|
|
+ if (proObj.objEqual(i, obj)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // 由于tmp.length初始的位置一直是0, 添加一个元素之后变为1,因此下标和长度每次相差1, 实现了实时插入数据的功能
|
|
|
- tmp[tmp.length] = this[i];
|
|
|
+ return false;
|
|
|
}
|
|
|
- return tmp;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -253,7 +187,73 @@ Array.prototype.intersect = function (target) {
|
|
|
// 只有找到相同的元素的时候返回的是true,其他情况都是返回的是false
|
|
|
return false;
|
|
|
});
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @desc : 取得原数组跟目标数组的交集
|
|
|
+ * @author : 周兴
|
|
|
+ * @date : 2022/11/15 17:21
|
|
|
+ */
|
|
|
+Array.prototype.intersection = function (arr) {
|
|
|
+ if (!this || !arr || arr.length == 0) return this;
|
|
|
+ // 如果第一个是对象,那么就按照对象数组来进行
|
|
|
+ if (typeof arr[0] == 'object') {
|
|
|
+ return this.filter(it => arr.has(it))
|
|
|
+ }
|
|
|
+ return this.filter(it => arr.includes(it))
|
|
|
+}
|
|
|
|
|
|
+/**
|
|
|
+ * @desc : 取得原数组跟目标数组的并集
|
|
|
+ * @author : 周兴
|
|
|
+ * @date : 2022/11/15 17:26
|
|
|
+ */
|
|
|
+Array.prototype.union = function (...arr) {
|
|
|
+ if (!this || !arr || arr.length == 0) return this;
|
|
|
+ return this.concat(...arr)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @desc : 取得原数组跟目标数组的差集
|
|
|
+ * @author : 周兴
|
|
|
+ * @date : 2022/11/15 17:26
|
|
|
+ * @param : arr:目标数组;cols,需要判断的列
|
|
|
+ */
|
|
|
+Array.prototype.minus = function (arr, cols = []) {
|
|
|
+ if (!this || !arr || arr.length == 0) return this;
|
|
|
+ // 如果第一个是对象,那么就按照对象数组来进行
|
|
|
+ if (typeof arr[0] == 'object') {
|
|
|
+ if (!cols || cols.length === 0) {
|
|
|
+ return this.filter(it => !arr.has(it))
|
|
|
+ } else {
|
|
|
+ let arrt = arr.map(it => {
|
|
|
+ let item = {}
|
|
|
+ cols.forEach(col => {
|
|
|
+ item[col] = it[col]
|
|
|
+ })
|
|
|
+ return item;
|
|
|
+ })
|
|
|
+ let thisTable = this.map(it => {
|
|
|
+ let item = {}
|
|
|
+ cols.forEach(col => {
|
|
|
+ item[col] = it[col]
|
|
|
+ })
|
|
|
+ return item;
|
|
|
+ })
|
|
|
+ return thisTable.filter(it => !arrt.has(it))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this.filter(it => !arr.includes(it))
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @desc : 数组的复制
|
|
|
+ * @author : 周兴
|
|
|
+ * @date : 2022/12/8 17:45
|
|
|
+ */
|
|
|
+Array.prototype.copy = function () {
|
|
|
+ if (!this) return this;
|
|
|
+ return JSON.parse(JSON.stringify(this));
|
|
|
}
|
|
|
|
|
|
/**
|