JS语法_集合
数组方法
forEach
// no-log
Array.prototype.forEach_ = function (cb) {
let len = this.length
for (let i = 0; i < len; i++) {
cb(this[i], i)
}
}
// log
let arr = ['', '', '']
arr.forEach_((item, index) => {
console.log('值:' + item + ' => ' + '索引:' + index)
})
为其加入中止功能
// no-log
Array.prototype.forEach_ = function (cb) {
let len = this.length
for (let i = 0; i < len; i++) {
let result = cb(this[i], i)
if (result === false) {
continue
}
}
}
// log
let arr = [1, 2, 120, 12, 3]
arr.forEach_((item, index) => {
if (item > 100) {
return false
}
console.log(item)
})
push
// no-log
Array.prototype.$push = function () {
for (let i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i]
}
return this.length
}
// log
let arr = [1, 2]
console.log(arr.$push(3, 4))
console.log(arr)
join
join 的效率要比字符串拼接高
// log
let str1 = '1'
let str2 = '2'
let str3 = '3'
let str4 = '4'
let str5 = '5'
let str6 = '6'
let arr = [str1, str2, str3, str4, str5, str6]
let strFinal = ''
function f1() {
for (let i = 0; i < arr.length; i++) {
strFinal += arr[i]
}
}
function f2() {
strFinal += arr.join('')
}
// test +
{
let start = new Date().getTime()
for (let i = 0; i < 100000; i++) {
f1()
}
let end = new Date().getTime()
console.log('字符串拼接耗时: ' + (end - start))
}
// test join
{
let start = new Date().getTime()
for (let i = 0; i < 10000; i++) {
f2()
}
let end = new Date().getTime()
console.log('join耗时: ' + (end - start))
}
类数组
必须有length属性
// log
let obj = {
1: 'a',
2: 'b',
length: 2,
}
Array.prototype.push.call(obj, 'c')
console.log(obj)
// log
let obj = {
2: 'a',
3: 'b',
length: 2,
push: Array.prototype.push,
}
obj.push('c', 'd')
console.log(obj)
Set
// log
// 去重
function unique(arr) {
return [...new Set(arr)]
}
// 交集
function intersection(arr1, arr2) {
let uniqueArr1 = [...new Set(arr1)]
return uniqueArr1.filter((item) => new Set(arr2).has(item))
}
// 并集
function union(arr1, arr2) {
return [...new Set([...arr1, ...arr2])]
}
// 差集
function diff(arr1, arr2) {
// 交集取反即可
let uniqueArr1 = [...new Set(arr1)]
return uniqueArr1.filter((item) => !new Set(arr2).has(item))
}
let x = [1, 2, 1, 0, 1, 2, 3, 4, 5, 1, 3, 4, 5, 2, 3, 2, 1, 2, 3, 4]
let y = [0, 10, 20, 30, 30, 40, 50]
console.log(unique(x))
console.log(intersection(x, y))
console.log(union(x, y))
console.log(diff(x, y))
console.log(diff(y, x))
JS语法_集合的更多相关文章
- python语法_集合
集合:不同的元素(不可hash)组合在一起的就叫做集合,去掉重复的,以空字符返回,无序的 可以分为可变集合和不可变集合(frozenset) 创建: s = set('gm gyx') print(s ...
- Python基本语法_集合set/frozenset_内建方法详解
目录 目录 前言 软件环境 可变集合Set set函数创建集合 创建空集合 集合元素的唯一性 集合推导式 set类型对象的内置方法 add增加一个元素 remove删除一个元素 pop随机删除并返回一 ...
- JS语法_类型
类型 JS 的数据类型 boolean number string undefined null symbol object TS 额外的数据类型 void BigInt 是一种内置对象,它提供了一种 ...
- JS语法_其他
严格模式 let obj = { name: 'oceans', } function f1() { with (obj) { console.log(name) } } function f2() ...
- JS 语法大全
来源http://blog.csdn.net/newegg2009/article/details/6230582 js语法2008年03月19日 星期三 11:14一.js的数据类型和变量 Java ...
- 01.JS语法规范、变量与常量
前言: 学习一门编程语言的基本步骤 (01)了解背景知识 (02)搭建开发环境 (03)语法规范 (04)常量和变量 2.JS的开发环境 (1)浏览器自带的JS解释器(js引擎) (2 ...
- Js获取后台集合List的值和下标的方法
Js获取后台集合List的值和下标的方法 转载自:http://blog.csdn.net/XiaoKanZheShiJie/article/details/47280449 首先用的是struts2 ...
- JS语法(二)
JS变量 var 变量名 = 变量值://自己会判断什么类型 一个好的编程习惯是,在代码开始处,统一对需要的变量进行声明. var name = “xiaosan”, age = 22, addres ...
- Sublime text3 JS语法检测工具安装及使用
Sublime text3 JS语法检测工具安装及使用 工具/原料 sublime text3 nodejs sublimeLinter sublimeLinter-jshint 方法/步骤 首先ct ...
随机推荐
- 2020-06-22:已知两个非负数的异或值为M,两数之和为N,求这两个数?
福哥答案2020-06-22: 1.遍历法时间复杂度:O(N)最好空间复杂度:O(1)平均空间复杂度:O(sqrt(N))最坏空间复杂度:O(N)[0,N/2]依次遍历,符合条件的就是需要的结果. 2 ...
- 思维导图概览SpringCloud
@ 目录 1.什么是微服务 1.1.架构演进 1.2.微服务架构 1.3.微服务解决方案 2.SpringCloud概览 2.1.什么是SpringCloud 2.1.SpringCloud主要组件 ...
- try-catch-finally异常处理:
java中三种实现多态的方案: 一:父类:普通类,普通方法: 子类:普通类,普通方法: 二:父类:抽象类,抽象方法: 子类:普通类,重写父类的抽象方法: 三:父类:接口类,抽象方法: 子类:普通类,实 ...
- 题解 洛谷 P3332
题目描述 权值线段树套线段树板子题 首先观察题目,判断为二维偏序问题 操作1为区间修改,所以一定是外部线段树维护权值,内部线段树维护所在区间,否则时间复杂度爆炸qwq 为方便查找,哈希时我采用哈希每个 ...
- [Kong 与 Konga与postgres数据库] 之 Kuberneres 部署
1.Kong的概述 Kong是一个clould-native.快速的.可扩展的.分布式的微服务抽象层(也称为API网关.API中间件或在某些情况下称为服务网格)框架.Kong作为开源项目在2015年推 ...
- Spring注解驱动开发04(给容器中注册组件的方式)
给容器中注册组件的方式 1. 组件注解标注 + 包扫描(适用于自己写的类) //控制层组件 @Controller public class PersonController { } //业务逻辑层组 ...
- 12. oracle 常用函数
一.字符函数字符函数是oracle中最常用的函数,我们来看看有哪些字符函数:lower(char):将字符串转化为小写的格式.upper(char):将字符串转化为大写的格式.length(char) ...
- CSP-J2019 把8个同样的球放在同样的5个袋子里,允许有的袋子空着不放,问共有多少种不同的分法?
把8个同样的球放在同样的5个袋子里,允许有的袋子空着不放,问共有多少种不同的分法? 提示:如果8个球都放在一个袋子里,无论是放哪个袋子,都只算同一种分法. 解析: 把问题合成,先思索5个袋子都不空的状 ...
- circos pipeline
# /usr/bin/env python# coding=utf-8#################################### Author : yunkeli# Version : ...
- MySQL数据库修改字段名、字段类型、字段长度
1.MySQL数据库中,修改字段SQL如下: alter table AppVersion change version versionCode varchar() DEFAULT NULL COMM ...