当前位置:网站首页>JS determines whether two values are equal, and compares any two values, including array objects
JS determines whether two values are equal, and compares any two values, including array objects
2022-06-25 14:19:00 【jason_ renyu】
Reading in leisure and boredom vuejs Source code , I found a strong comparison method in it. Even array objects can be compared correctly ; Later, you can get whether a value exists in the data , A stronger one indexOf, If it's useful to you, welcome to like, collect and share .
Don't talk much , Go straight to the code :
/** * Determine whether it is the base type object( This is based on type judgment , So arrays and objects are indistinguishable , Only for the following comparison ) * @param {any} obj Pass in the data type to be judged * @returns by object For the true, Otherwise false */
function isObject (obj) {
return obj !== null && typeof obj === 'object'
}
/** * Determine if two values are equal * @param {any} a Any data type * @param {any} b Any data type * @returns true For consistency , false Is not equal */
function looseEqual (a, b) {
if (a === b) {
return true }
var isObjectA = isObject(a);
var isObjectB = isObject(b);
if (isObjectA && isObjectB) {
try {
var isArrayA = Array.isArray(a);
var isArrayB = Array.isArray(b);
if (isArrayA && isArrayB) {
return a.length === b.length && a.every(function (e, i) {
return looseEqual(e, b[i])
})
} else if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime()
} else if (!isArrayA && !isArrayB) {
var keysA = Object.keys(a);
var keysB = Object.keys(b);
return keysA.length === keysB.length && keysA.every(function (key) {
return looseEqual(a[key], b[key])
})
} else {
/* istanbul ignore next */
return false
}
} catch (e) {
/* istanbul ignore next */
return false
}
} else if (!isObjectA && !isObjectB) {
return String(a) === String(b)
} else {
return false
}
}
/** * Get in collection , The position of the current value * @param {any} arr Incoming collection * @param {any} val To query whether the value of the position in the collection * @returns -1 For there is no such thing ,0 or 0 The above is the existing position subscript */
function looseIndexOf (arr, val) {
for (var i = 0; i < arr.length; i++) {
if (looseEqual(arr[i], val)) {
return i }
}
return -1
}
Close test effectively , If you have any questions, please leave a message .( The author is lazy to carry , It's not changed to es6 The way , Make changes if necessary )
边栏推荐
- It's not easy to understand the data consistency of the microservice architecture for the first time after six years as a programmer
- Sigmoid function sigmoid derivation
- [world history] Episode 1: people in the Stone Age
- 楼宇自动化专用BACnet网关BL103
- Supplementary inheritance and strict mode
- sigmoid函数sigmoid求导
- What if the CPU temperature of Dell computer is too high
- Complete and detailed compilation of experimental reports
- Nr-arfcn and channel grid, synchronous grid and GSCN
- Installation and removal of MySQL under Windows
猜你喜欢

BACnet gateway bl103 for building automation

Numpy库使用入门

两种方法实现pycharm中代码回滚到指定版本(附带截图展示)

Share the code technology points and software usage of socket multi client communication

Les neuf caractéristiques de la parole et les neuf temps en anglais

k线图24种经典图解(影线篇)

Getting started with numpy Library

‘nvidia-smi‘ 不是内部或外部命令,也不是可运行的程序或批处理文件

电脑必须打开的设置

Getting started with shell variables
随机推荐
Table de hachage, conflit de hachage
合宙Air32F103CBT6开发板上手报告
Solving error: creating window glfw error: glew initialization error: missing GL version
"Mobile cloud Cup" computing power network application innovation competition is in hot registration!
Hash table, hash conflict
一次性讲清楚 Handler 可能导致的内存泄漏和解决办法 | 开发者说·DTalk
golang项目依赖管理工具go vendor,go mod
算力&NFT交易平台F3.xyz旗下独家权益NFT项目Hash Eagle将盛大发行
As a software testing engineer, how do you think to ensure software quality?
Supplementary inheritance and strict mode
Complete and detailed compilation of experimental reports
laravel8实现图片验证码
Does stream even have application advanced learning? As a programmer, you know what
Discriminative v.s.Generative
[untitled]
shell 数组
Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
Classifier and cross entropy loss function
Which is better and safer, GF easy gold rush or flush
Page 112 machine learning - review of fundamentals of mathematics pptx