let obj = {
"stepInfo_companyChannel": [
"1",
"2",
"3"
],
"stepInfo_promotionSteps<0>_companyFirstChargeAmount": 12,
"stepInfo_promotionSteps<0>_companyFirstChargeAmount1": 122,
"stepInfo_promotionSteps<0>_mainStep": true,
"stepInfo_promotionSteps<0>_returnBcoupon": true,
"stepInfo_promotionSteps<0>_smsTemplateId": false,
"stepInfo_promotionSteps<0>_push": false,
"stepInfo_promotionSteps<0>_returnBcoupon<1>_batchId": "1234",
"stepInfo_promotionSteps<0>_returnBcoupon<1>_number": 1233,
"stepInfo_promotionSteps<0>_returnBcoupon<0>_fastCarBatchId": "1228",
"stepInfo_promotionSteps<0>_returnBcoupon<0>_fastCarNumber": 124,
"stepInfo_promotionSteps<0>_returnBcoupon<0>_batchId": "11",
"stepInfo_promotionSteps<0>_returnBcoupon<0>_number": 129,
"stepInfo_promotionSteps<0>_returnBcoupon<0>_replaceFastCarCoupon": 1,
"stepInfo_promotionSteps<1>_companyFirstChargeAmount": 99,
"stepInfo_promotionSteps<1>_companyFirstChargeAmount1": 990,
"stepInfo_promotionSteps<1>_mainStep": true,
"stepInfo_promotionSteps<1>_returnBcoupon": true,
"stepInfo_promotionSteps<1>_sms": false,
"stepInfo_promotionSteps<1>_push": false,
"stepInfo_promotionSteps<1>_returnBcoupon<1>_batchId": "555",
"stepInfo_promotionSteps<1>_returnBcoupon<1>_number": 777,
"stepInfo_promotionSteps<1>_returnBcoupon<1>_replaceFastCarCoupon": 1,
"stepInfo_promotionSteps<1>_returnBcoupon<0>_fastCarBatchId": "88",
"stepInfo_promotionSteps<1>_returnBcoupon<0>_fastCarNumber": 665,
"stepInfo_promotionSteps<1>_returnBcoupon<1>_fastCarBatchId": "44",
"stepInfo_promotionSteps<1>_returnBcoupon<1>_fastCarNumber": 444,
"stepInfo_stepCrowdTag": "1001271955",
"stepInfo_promotionSteps<1>_returnBcoupon<0>_batchId": "9999",
"stepInfo_promotionSteps<1>_returnBcoupon<0>_number": 1292929,
"stepInfo_promotionSteps<1>_returnBcoupon<0>_replaceFastCarCoupon": 1
}
const replaceNullProperty = (obj) => {
let nullData = [undefined,null,'null',"undefined",'']
for( let i in obj ){
if(obj[i].constructor === Object){
Object.keys(obj[i]).map(item=>{
replaceNullProperty(obj[i][item])
})
}else if(obj[i].constructor === Array){
let len = obj[i].length
for(let j = 0; j < len ; j++){
if( nullData.includes(obj[i][j])){
obj[i][j] = {}
}else if(obj[i][j].constructor === Array || obj[i][j].constructor === Object){
replaceNullProperty(obj[i][j])
}
}
}
}
return obj
}
function _basePath(path) {
if (Array.isArray(path)) return path
return path.replace(/\</g, '_').replace(/\>/g, '').split('_')
}
function setObj(object, path, value) {
if (typeof object !== 'object') return object;
_basePath(path).reduce((o, k, i, _) => {
if (i === _.length - 1) {
o[k] = value
return
} else if (k in o) {
return o[k]
} else {
o[k] = /^[0-9]{1,}$/.test(_[i + 1]) ? [] : {}
return o[k]
}
}, object)
return object;
}
const getRawType = (val) => {
return Object.prototype.toString.call(val).slice(8,-1)
}
const isPlainObject = (val) => {
return getRawType(val) === 'Object'
}
const isPlainObjectOrArray = (val) => {
return isPlainObject(val) || Array.isArray(val)
}
let newObj = []
Object.keys(obj).map((item,index)=>{
newObj.push(setObj({},item , obj[item]))
})
const merge = (object, ...sources) => {
for (const source of sources) {
for (const key in source) {
if (source[key] === undefined && key in object) {
continue
}
if (isPlainObjectOrArray(source[key])) {
if (isPlainObjectOrArray(object[key]) && getRawType(object[key]) === getRawType(source[key])) {
if (isPlainObject(object[key])) {
merge(object[key], source[key])
} else {
object[key] = mergeArr(object[key],source[key])
}
} else {
object[key] = source[key]
}
} else {
object[key] = source[key]
}
}
}
}
function mergeArr(arr1,arr2){
const key2=Object.keys(arr2[0])[0]
if( Array.isArray(arr2[0][key2]) ){
Object.keys(arr1[0]).forEach(item=>{
if(item==key2){
if(Array.isArray(arr1[0][key2])){
mergeArr(arr1[0][key2],arr2[0][key2])
}else{
arr1[0][key2]=arr2[0][key2]
}
}
})
}else{
Object.assign(arr1[0],arr2[0])
}
return arr1
}
let noNullArr = replaceNullProperty(newObj)
let object = {}
merge(object,...noNullArr);
console.log(";藕节",JSON.stringify(object))
console