function unique(arr) {
if (!Array.isArray(arr)) {
console.log('type error!')
return
}
var array = [];
for (var i = 0; i < arr.length; i++) {
if (array.indexOf(arr[i]) === -1) {
array.push(arr[i])
}
}
return array;
}
var arr = [1, 2, 2, 3, 4]
console.log(unique(arr))
function group(array, subGroupLength) {
let index = 0;
let newArray = [];
while (index < array.length) {
newArray.push(array.slice(index, index += subGroupLength));
}
return newArray;
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(group(arr, 3))
Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
Array.prototype.remove = function (val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
arrays.remove('要删除的元素');
function sortArr(arr, str) {
var _arr = [],
_t = [],
_tmp;
arr = arr.sort(function (a, b) {
var s = a[str],
t = b[str];
return s < t ? -1 : 1;
});
if (arr.length) {
_tmp = arr[0][str];
}
for (var i in arr) {
if (arr[i][str] === _tmp) {
_t.push(arr[i]);
} else {
_tmp = arr[i][str];
_arr.push(_t);
_t = [arr[i]];
}
}
_arr.push(_t);
return _arr;
}
sortArr(data, "name")
- 今天看到一篇报道里面的专家说:在狗的眼中,人类所做的一切动作都是慢动作。我恍然大悟,怪不得老板总说我干活慢!.