当前位置:网站首页>js 常用的 方法
js 常用的 方法
2022-07-21 05:23:00 【星期天!】
Set 对象的作用
1 数组去重 去掉重复的 (String / Number)
const arr1 = [0, 0, 1, 3, 4, 5, 2, 3, 7]
const removeDuplicates = (arr) => [...new Set(arr)]
console.log(removeDuplicates(arr1)) // [0, 1, 3, 4, 5, 2, 7]
**2 并集 **
const a = [1, 2, 3]
const b = [2, 3, 4]
const c = [4, 5, 6]
const union1 = new Set([...a, ...b])
const union2 = new Set([...a, ...b, ...c])
console.log(union1); // {1, 2, 3, 4}
console.log(union2); // {1, 2, 3, 4, 5, 6}
3 交集
const d = new Set([1, 2, 3])
const e = new Set([4, 3, 2])
const intersect = new Set([...d].filter(item => e.has(item)))
console.log(intersect); // {2, 3}
** 4 差集**
const f = new Set([1, 2, 3])
const g = new Set([4, 3, 2])
const difference = new Set([...f].filter(item => !g.has(item)))
console.log(difference); // {1}
** 5 判断一个数组是否为空**
const a = [1,2,3]
const inNotEmpat = arr => Array.isArray(arr) && arr.length > 0;
console.log(inNotEmpat) // true
** 6 合并两个数组**
const arr1 = [1,2,3]
const arr2 = [4,5,6]
const arr3 = (a,b) => a.concat(b)
const arr4 = (a,b) => [...a,...b]
console.log(arr3(arr1,arr2) // [1,2,3,4,5,6]
7 判断一个数是奇数还是偶数
const isEven = num => num % 2 === 0
console.log(isEven(1)) // false
console.log(isEven(2)) // true
8 获取一个数组的平均值
const arr = [1,2,3,4]
const everage = (...args) => args.reduce((a,b) => a + b ) / args.length
console.log(everage(arr)) // 2.5
** 9 使用 JSON .stringify 格式化输出**
const obj = {
age:18,
name:"星期天",
height:180
}
console.log(JSON.stringify(obj,null,4))
10 字符串数字 快速转 number
const string = "1"
const num = +string
console.log(typeof num) // number
11 获取对象的全部属性值 使用 Object.values
const deps = {
'采购部':[1,2,3],
'人事部':[5,8,12],
'行政部':[5,14,79],
'运输部':[3,64,105],
}
let member = Object.values(deps).flat(Infinity);
console.log(member) // [1,2,3,5,8,12,5,14,79,3,64,105]
12 找出数组中 总和 最大 最小值
const arr = [1,2,3,4,5];
const total = arr.reduce((prev, curr) => prev + curr); // 总和 15
const max = arr.reduce((per,cur) => per > cur ? per : cur); // 最大值 5
const min = arr.reduce((per,cur) => per < cur ? per : cur); // 最小值 1
13 数组排序
const arr = [2,3,4,5,1,8,4];
const newArr = arr.sort((a,b) => a - b);
console.log(newArr); // [ 1, 2, 3, 4, 4, 5, 8 ]
14 数组中过滤掉虚假值
const arr = [3,1,'',false]
const newArr = arr.filter(item => item)
console.log(newArr)
15数组去重
const arr = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9];
const newArr = arr.filter((item,index) => {
return arr.indexOf(item) === index;
})
console.log(newArr); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
边栏推荐
- NAT网络地址转换协议
- Scala installation
- [natural language processing and text analysis] comprehensive scanning method and item by item flipping method in basic information retrieval technology.
- Form form label
- Comparison table of ZH CN, en US, Zh TW and other language (Culture) codes with countries and regions
- 【Grafana】CentOS下安装MySQL+Grafana添加MySQL数据源
- Go语言之return语句的几种用法
- 【特征转化】特征转化是为了保证很小的信息损失,但有高品质的预测结果。
- Network type division
- TCP/IP五层和七层模型总结(建议全文背诵)
猜你喜欢
高并发场景下请求合并(批量)
[feature learning] feature learning based on deep learning and word embedding
1. Basic concepts of machine learning
【自然语言处理和文本分析】基础信息检索:签名文件技术,进阶信息检索:向量空间技术(目前主流的搜索引擎在用的技术)
DNS domain name resolution
OSPF实验
OSPF serial number
Several common login debugging methods
一顿饭的时间,教你怎样快速使用 动态代理ip 做一个获取Steam 热销商品 的方法
RFC document download
随机推荐
TCP/IP五层和七层模型总结(建议全文背诵)
[feature learning] use association rules and neural networks to establish new features
Summary of tcp/ip five layer and seven layer models (it is recommended to recite the full text)
路由器的工作原理
R5为ISP,只能进行IP地址配置,其所有地址均配为共有IP地址
1.机器学习的基础概念
在vscode里配置rust时遇到error:linking with ‘x86_64-w64-mingw32-gcc‘failed:exit code:1
Form form label
Introduction and configuration of raid
【自然语言处理与文本分析】PCA文本降维。奇异值分解SVD,PU分解法。无监督词嵌入模型Glove。有案例的将文本非结构化数据转化为结构化数据的方法。
[100 unity practical skills] | list in C The find method looks for the first element that meets the requirements and returns
[natural language processing and text analysis] PCA text dimensionality reduction. Singular value decomposition SVD, Pu decomposition method. Unsupervised word embedding model glove. The method of tra
【自然語言處理與文本分析】用兩個項目案例介紹文本挖掘方法論。
HCIA summary
scala安装
基于无法安装64位版本的visio,因为在您的PC上找到了以下32位程序的解决办法
1. Basic concepts of machine learning
VRRP virtual routing Redundancy Protocol
[natural language processing and text analysis] introduce the text mining methodology with two project cases.
vmware workstation pro 16安装出现“setup failed to generate the ssl keys necessary to run vmware”