标签:indexof arrays const let new ble 数组 yun 序号
序号 | 内容 | 是否明白 | 备注 |
---|---|---|---|
数组 | |||
1. | 数组去重(1) | 是 | time:2019/04/09 |
2. | 数组去重(2) | 是 | time:2019/04/09 |
3. | 数组去重(3)es6 | 是 | time:2019/04/10 |
4. | 判断两个数组是否相等 | 是 | time:2019/04/09 |
/**
* 数组去重
* @param {Array}arr is
* @returns {*} is
*/
function arrayClear(arr) {
const arr2 = [];
if (arr instanceof Array) {
for (let i = 0, len = arr.length; i < len; i++) {
if (arr2.indexOf(arr[i]) < 0) {
arr2.push(arr[i]);
}
}
return arr2;
}
return false;
}
module.exports = arrayClear;
/**
* @desc 最简单数组去重法
* @param {Array} arr is
* @return {Boolean} is
*/
function arrayUnique(arr) {
const n = [];
for (let i = 0; i < arr.length; i++) {
if (n.indexOf(arr[i]) === -1) n.push(arr[i]);
}
return n;
}
module.exports = arrayUnique;
/**
* @desc es6数组去重法
* @param {Array} arr is
* @return {Boolean} is
*/
function arraySet(arr) {
let set = new Set(arr);
return set;
}
module.exports = arrayUnique;
/**
*
* @desc 判断两个数组是否相等
* @param {Array} arr1 is
* @param {Array} arr2 is
* @return {Boolean} is
*/
function arrayEqual(arr1, arr2) {
if (arr1 === arr2) return true;
if (arr1.length !== arr2.length) return false;
for (let i = 0; i < arr1.length; ++i) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
}
module.exports = arrayEqual;
标签:indexof arrays const let new ble 数组 yun 序号
原文地址:https://www.cnblogs.com/JinXinYuan/p/10682414.html