Ok So For Example let's say this is My Object.
const myObj = {
cf_retryAttempts:0,
cf_amount:1,
cf_event:"SUBSCRIPTION_NEW_PAYMENT",
cf_eventTime:"2022-01-10 10:03:50",
cf_paymentId:1,
cf_referenceId:2,
cf_subReferenceId:3,
}
If I use JSON.stringify(myObj) it returns something like {"cf_retryAttempts":"0"...}
But I don't want that what I want is a complete string without any doubleQuotes, commas or colon separators between key value. so what I'm expecting is something like:
const string = cf_amount1cf_eventSUBSCRIPTION_NEW_PAYMENTcf_eventTime2022-01-10 10:51:02cf_paymentId1cf_referenceId2cf_retryAttempts0cf_subReferenceId3
If you want to understand more please check the docs as to understand what I'm trying to achieve.
Link: https://docs.cashfree.com/docs/webhooks-1#verify-signature
Language: JS
CodePudding user response:
You can iterate all its keys and values, also recursively if you encounter an object.
const myObj = {
cf_retryAttempts: 0,
cf_amount: 1,
cf_event: "SUBSCRIPTION_NEW_PAYMENT",
cf_eventTime: "2022-01-10 10:03:50",
cf_paymentId: 1,
cf_referenceId: 2,
cf_subReferenceId: 3,
cf_inner: {
'this': 'that'
}
}
function iterate(obj) {
var result = ""
Object.entries(obj).forEach(function([key, value]) {
if (typeof value === 'object' && value !== null) {
result = (key iterate(value))
} else {
result = (key value)
}
})
return result;
}
console.log(iterate(myObj))
CodePudding user response:
simply ?
cf_
elements are sorted.
const myObj =
{ cf_retryAttempts : 0
, cf_amount : 1
, cf_event : 'SUBSCRIPTION_NEW_PAYMENT'
, cf_eventTime : '2022-01-10 10:03:50'
, cf_paymentId : 1
, cf_referenceId : 2
, cf_subReferenceId : 3
, signature : 'tT9pXZkT2LuDzXacYDUaur7EX3dJgNKcITHQng44tns='
}
function objStr( {signature, ...cf_x })
{
return Object.entries(cf_x)
.sort((a,b)=>a[0].localeCompare(b[0]))
.reduce((r,[e,k])=> r e k,'')
}
console.log( objStr(myObj) )