目录
一、判断数组中是否有重复数据
1.普通数据
const arr = ['111', '222', '333', '444', '555']; //判断数组元素是否有重复
function getisRepeat(arr) {
var hash = {};
for (var i in arr) {
if (hash[arr[i].name]) {
//hash 哈希
return true;
}
hash[arr[i].name] = true;
}
return false;
}
2.对象元素数组
要判断下方这个数组中,name是否有重复的数据
const arr=[
{name:"张三3",age:12},
{name:"张三2",age:12},
{name:"张三",age:12},
{name:"张三1",age:12}
];
(1)先利用ES6语法Set将数组去重,之后再原数组比较长度,若长度小于原数组,则说明数组有重复值
const arr = [{
name: "张三3",
age: 12
}, {
name: "张三2",
age: 12
}, {
name: "张三0",
age: 12
}, {
name: "张三1",
age: 12
}];
const newListLength = new Set(arr.map(item => item.name)).size;
const listLength = arr.length;
if (listLength > newListLength) {
console.log("重复");
}
(2)利用findIndex或者indexOf查到的下标和当前循环的下标对比是否相等
// indexOf查找是否有重复的
const arr = [{
name: "张三3",
age: 12
}, {
name: "张三2",
age: 12
}, {
name: "张三1",
age: 12
}, {
name: "张三1",
age: 12
}];
const newArr = arr.map(item => item.name);
const isRepeat = newArr.some((item, index, arr) => arr.indexOf(item) != index);
if (isRepeat) {
console.log("重复");
}
const arr = [{
name: "张三3",
age: 12
}, {
name: "张三2",
age: 12
}, {
name: "张三1",
age: 12
}, {
name: "张三1",
age: 12
}];
const newArr = arr.map(item => item.name);
const isRepeat = newArr.some((x, index, arr) => arr.findIndex(y => y == x) != index);
if (isRepeat) {
console.log("重复");
}
二、数组去重
1.普通数组
//1.利用Set 方法去重
let nums = [1, 4, 3, 2, 5, 9, 8, 3, 2, 3, 4, 8]
function removeDuplicate(nums) {
let set = new Set(nums);
return Array.from(set)
// return Array.from(new Set(nums));
}
console.log(removeDuplicate(nums));
//2.利用Array indexOf, lastIndexOf ,includes,some
//arr.indexOf(searchElement[, fromIndex]) 首个被找到的元素在数组中的索引位置; 若没有找到则返回 -1
//arr.lastIndexOf(searchElement[, fromIndex]) 数组中该元素最后一次出现的索引,如未找到返回-1。
//arr.includes(valueToFind[, fromIndex]) 返回一个布尔值 Boolean ,如果在数组中找到了(如果传入了 fromIndex ,表示在 fromIndex 指定的索引范围中找到了)则返回 true 。
//arr.some(callback(element[, index[, array]])[, thisArg]) 数组中有至少一个元素通过回调函数的测试就会返回true;所有元素都没有通过回调函数的测试返回值才会为false。
function removeDuplicate(nums) {
let n = nums.length;
let newArr = []
for (let i = 0; i < n; i++) {
//if(newArr.indexOf(nums[i])===-1){
// if(newArr.lastIndexOf(nums[i])===-1){
//if (newArr.includes(nums[i]) === false) {
if (newArr.some((num) => num === nums[i]) === false) {
newArr.push(nums[i])
}
}
return newArr;
}
// 3.利用 array.filter
// var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
// 一个新的、由通过测试的元素组成的数组,如果没有任何数组元素通过测试,则返回空数组。
function removeDuplicate(nums) {
return nums.filter((num, index, arr) => {
return arr.indexOf(num) === index;
})
}
console.log(removeDuplicate(nums));
// 4.利用 Map myMap.set(key, value);
// myMap.has(key); 如果指定元素存在于Map中,则返回true。其他情况返回false
function removeDuplicate(nums) {
let n = nums.length;
let map = new Map();
for (let num of nums) {
if (!map.has(num)) {
map.set(num, 0);
}
}
return Array.from(map.keys());
}
console.log(removeDuplicate(nums));
// 5.利用 reduce
// arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
// 1)如果没有提供initialValue,reduce 会从索引1的地方开始执行 callback 方法,跳过第一个索引。如果提供initialValue,从索引0开始。
// 2)如果数组为空且没有提供initialValue,会抛出TypeError 。 3)如果数组仅有一个元素(无论位置如何)并且没有提供initialValue,
// 或者有提供initialValue但是数组为空,那么此唯一值将被返回并且callback不会被执行。
function removeDuplicate(nums) {
let res = nums.reduce((acc, cur) => {
if (acc.indexOf(cur) === -1) {
acc.push(cur);
}
return acc;
}, []);
return res;
}
console.log(removeDuplicate(nums));
2.对象元素数组
const data = [
{ id: 1, name: '张三', age: 15, },
{ id: 2, name: 'John', age: 18, },
{ id: 3, name: '李四', age: 18, },
{ id: 1, name: '张三', age: 15, },
{ id: 4, name: 'Jack', age: 18, },
{ id: 5, name: '王五', age: 10, },
{ id: 4, name: 'Jack', age: 18, },
{ id: 2, name: 'John', age: 18, },
{ id: 22, name: 'John', age: 18, },];
//1.通过hash方法
let hash = {};
const data2 = data.reduce((preVal, curVal) => {
hash[curVal.id] ? "" : (hash[curVal.id] = true && preVal.push(curVal));
return preVal;
}, []);
console.log("data2 :>> ", data2);
//2.通过forEach再通过some方法判断数组是否包含当前对象id,不包含则添加
function some(data) {
let some = [];
data.forEach(el => {
if (!some.some(e => e.id == el.id)) {
some.push(el);
}
});
console.log('%c [ some去重结果 ]', 'font-size:13px; background:pink; color:#bf2c9f;', some);
}
//3.通过forEach再通过find方法判断数组是否包含当前对象id,不包含则添加
function find() {
let find = [];
data.forEach(el => {
if (!find.find(e => e.id == el.id)) {
find.push(el);
}
});
console.log('%c [ find去重结果 ]', 'font-size:13px; background:pink; color:#bf2c9f;', find);
}
//4.通过reduce方法,通过定义的obj,判断obj[next.id] 是否存在,存在设置为“”,不存在则push
function reduce() {
let obj = {};
let reduce = [];
reduce = data.reduce(function (item, next) { //item为没有重复id的数组,next为当前对象
obj[next.id] ? '' : (obj[next.id] = true && item.push(next));
return item;
}, []);
console.log('%c [ reduce去重结果 ]', 'font-size:13px; background:pink; color:#bf2c9f;', reduce);
}
//5.通过for循环遍历,再通过some方法判断数组是否包含当前对象id,不包含则添加
function forAway() {
let forData = [];
for (let i = 0; i < data.length; i++) {
if (!forData.some(e => e.id == data[i].id)) forData.push(data[i]);
}
console.log('%c [ forData去重结果 ]', 'font-size:13px; background:pink; color:#bf2c9f;', forData);
}
some(data)
find(data)
reduce(data)
forAway(data)
三、将数组拆分成几个数组
该函数的第一个参数为 array(需要拆分的数组),第二个参数为size(每组数组多少个),该函数直接返回新生成的数组(该数组的元素就是拆分的数组,拆分了几个数组,就有几个元素)
// size每组数组多少个,如:10
// array需要拆分的数组
function splitArray(array, size){
let data = [];
for (let i = 0; i < array.length; i += size) {
data.push(array.slice(i, i + size))
}
return data
}
一些常用数组方法介绍:常见的几种数组方法_每日一问终成大佬的博客-优快云博客