合并两个未知数据类型的变量
对象只能是一级,内嵌多层则为true
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>合并两个未知数据类型的变量(对象只能是一级,内嵌多层则为true)</title>
</head>
<body>
<button onclick="testFn1()">testFn1</button>
<button onclick="testFn2()">testFn2</button>
<button onclick="testFn3()">testFn3</button>
<script>
function testFn1 () {
// 1.val1:string, val2:array,object
const t1 = 'test1'
const t2 = ['hide']
const t3 = {
hide: true, hide2: false }
// debugger
const val1 = fnObj.megerVariable(t1, t2)
// debugger
const val2 = fnObj.megerVariable(t1, t3)
// debugger
console.log('1.val1', val1) // 1.val1 test1 hide
console.log('1.val2', val2) // 1.val2 test1 hide
}
function testFn2 () {
// 2.val1:array, val2:string,object
const t1 = ['test1']
const t2 = 'hide2'
const t3 = {
ttt31: true, ttt32: false, ttt33: {
ttt4: true } }
// debugger
const val1 = fnObj.megerVariable(t1, t2)
// debugger
const val2 = fnObj.megerVariable(t1, t3)
// debugger
console.log('2.val1', val1) // 2.val1 (2) ["test1", "hide2"]
console.log('2.val2', val2) // 2.val2 (3) ["test1", "ttt31", "ttt33"]
}
function testFn3 () {
// 3.val1:object, val2:string,array
const t1 = {
test1: true, test2: false }
const t2 = 'hide2'
const t3 = ['ttt31', [[['ttt32']]]]
// debugger
const val1 = fnObj.megerVariable(t1, t2)
// debugger
const val2 = fnObj.megerVariable(t1, t3)
// debugger
console.log('3.val1', val1) // 3.val1 {test1: true, test2: false, hide2: true}
console.log('3.val2', val2) // 3.val2 {test1: true, test2: false, ttt31: true, ttt32: true}
}
const fnObj = {
// 合并两个未知数据类型的变量(对象只能是一级,内嵌多层则为true)
megerVariable (val1, val2) {
const noVal1 = this.isNull(val1)
const noVal2 = this.isNull(val2)
// debugger
if (!noVal1 && !noVal2) {
// 只有当两个都有值,才有合并的意义
const type1 = this.getType(val1)
const type2 = this.getType(val2)
if (type1 === 'object' || type1 === 'array') {
val1 = JSON.parse(JSON.stringify(val1))
}
if (type2 === 'object' || type2 === 'array') {
val2 = JSON.parse(JSON.stringify(val2))
}
if (type1 !== type2) {
// 数据类型不同
let value = ''
if (type1 === 'string') {
// 1.val1:string, val2:array,object
value = val1
if (type2 === 'array') {
value += ' ' + val2.join(',')
} else if (type2 === 'object') {
for (const k in val2) {
if (val2[k]) {
// 为true才允许添加
value += ' ' + k + ' '
}
}
}
} else if (type1 === 'array') {
// 2.val1:array, val2:string,object
value = val1
if (type2 === 'string') {
value.push(val2)
} else if (type2 === 'object') {
for (const k in val2) {
if (val2[k]) {
// 为true才允许添加
value.push(k)
}
}
} else if (type2 === 'array') {
value = val1.concat(val2)
}
} else if (type1 === 'object') {
// 3.val1:object, val2:string,array
if (type2 === 'string') {
val1[val2] = true
} else if (type2 === 'array') {
val2.forEach(v => {
val1[v] = true
})
}
value = val1
}
return value
} else {
// 数据类型相同
let val = ''
// debugger
if (type1 === 'string') {
val = val1 + ' ' + val2
} else if (type1 === 'object') {
val = val1
for (const k in val2) {
if (val2[k]) {
// 为true才允许添加
val[k] += ' ' + k + ' '
}
}
} else if (type1 === 'array') {
val = val1.concat(val2)
}
return val
}
} else {
return !noVal1 ? val1 : val2
}
},
/**
* 判断是否为空
*/
isNull (val) {
if (val instanceof Array) {
if (val.length === 0) return true
} else if (val instanceof Object) {
const arr = Object.keys(val) // 判断对象是否为空
if (arr.length <= 0) return true
} else {
const str = '' + val
if (str === '[]' || str === '{}') {
return true
} else if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') return true
return false
}
return false
},
/**
* 获取对象属性类型
* @param e
* @returns {string}
*/
getType (value) {
var type = ''
if (typeof value !== 'object') {
type = typeof value
} else {
if (value instanceof Array) {
type = 'array'
} else if (value instanceof Object) {
type = 'object'
} else if (value instanceof Function) {
type = 'function'
} else if (typeof value === 'string') {
type = 'string'
} else {
type = 'null'
}
}
return type
}
}
</script>
</body>
</html>