1.计算一个数组的重复数据的次数
function countDuplicates ( arr ) {
const frequency = { } ;
const duplicates = { } ;
arr. forEach ( item => {
frequency[ item] = frequency[ item] ? frequency[ item] + 1 : 1 ;
} )
arr. forEach ( item => {
frequency[ item] = ( frequency[ item] || 0 ) + 1 ;
} ) ;
for ( const key in frequency) {
if ( frequency. hasOwnProperty ( key) && frequency[ key] > 1 ) {
duplicates[ key] = frequency[ key] ;
}
}
return duplicates;
}
const arr = [ 2 , 2 , 3 , 4 , 2 , 3 , 4 , 5 , 6 , 4 , 5 , 6 , 7 ] ;
const duplicates = countDuplicates ( arr) ;
console. log ( duplicates) ;
2.数组去重
const array = [ 1 , 2 , 2 , 3 , 4 , 4 , 5 ] ;
const uniqueArray = [ ... new Set ( array) ] ;
console. log ( uniqueArray) ;
const array = [ 1 , 2 , 2 , 3 , 4 , 4 , 5 ] ;
const uniqueArray = array. filter ( ( item, index ) => array. indexOf ( item) === index) ;
console. log ( uniqueArray) ;
3.小数位超过两位,则保留两位小数
function formatNumber ( number : any) {
if ( ! number || Number. isNaN ( number) ) {
return number;
}
if ( Number. isInteger ( number) || number. toFixed ( 2 ) === number. toString ( ) ) {
return number. toString ( ) ;
}
return number. toFixed ( 2 ) ;
}
4.多层数组对象,取出指定值的该对象
function findObject ( obj : any, target : any) {
if ( Array. isArray ( obj) ) {
for ( let i = 0 ; i < obj. length; i++ ) {
const result : any = findObject ( obj[ i] , target) ;
if ( result) {
return result;
}
}
} else if ( typeof obj === "object" && obj !== null ) {
for ( let key in obj) {
if ( obj[ key] === target) {
return obj;
}
const result : any = findObject ( obj[ key] , target) ;
if ( result) {
return result;
}
}
}
return null ;
}
const data = [
{
id : 1 ,
name : 'John' ,
children : [
{
id : 2 ,
name : 'Alice' ,
children : [
{
id : 3 ,
name : 'Bob' ,
} ,
] ,
} ,
{
id : 4 ,
name : 'Charlie' ,
} ,
] ,
} ,
{
id : 5 ,
name : 'Dave' ,
} ,
] ;
const result = findObject ( data, 'Bob' ) ;
console. log ( result) ;
5.将数组对象根据指定名称分类
function groupByDate ( data : Array< Object> , properties : any) {
let groupData = new Map ( ) ;
data. forEach ( ( item : any) => {
if ( ! groupData. has ( item[ properties] ) ) {
groupData. set ( item[ properties] , [ item] ) ;
} else {
groupData. get ( item[ properties] ) . push ( item) ;
}
} ) ;
return groupData;
}
let data = [
{
calss : 'a' ,
value : 1 ,
name : '名'
} ,
{
calss : 'a' ,
value : 2 ,
name : '发'
} ,
{
calss : 'b' ,
value : 3 ,
name : '是'
} ,
{
calss : 'c' ,
value : 4 ,
name : '呃'
}
]
let arr2 = Array. from ( groupByDate ( data, "class" ) ) ;
arr2 = [
[
'a' ,
[
{
calss : 'a' ,
value : 1 ,
name : '名'
} ,
{
calss : 'a' ,
value : 2 ,
name : '发'
} ,
]
] ,
[
'b' ,
[
{
calss : 'b' ,
value : 3 ,
name : '是'
}
]
] ,
[
'c' ,
[
{
calss : 'c' ,
value : 4 ,
name : '呃'
}
]
] ,
]
6.使用递归处理删除具有空 children 数组的层级
let data = [
{
label : '1' ,
value : '1' ,
children : [
{
label : '2' ,
value : '2' ,
children : [ ]
}
]
} ,
{
label : '3' ,
value : '3' ,
children : [ ]
}
]
removeEmptyChildren ( arr ) {
arr. forEach ( ( item ) => {
if ( item. children && item. children. length > 0 ) {
this . removeEmptyChildren ( item. children) ;
} else {
delete item. children;
}
} ) ;
return arr;
} ,
let arr = removeEmptyChildren ( data)
arr= [
{
label : '1' ,
value : '1' ,
children : [
{
label : '2' ,
value : '2' ,
}
]
} ,
{
label : '3' ,
value : '3' ,
}
]