Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 527 characters in body
Source Link
loretoparisi
  • 16.3k
  • 12
  • 113
  • 159
function/** * Object to string * @param {*} obj * @param {*} k_sep keys separator * @param {*} v_sep values separator * @returns */  var objectToString: function (obj, sepk_sep = '=', v_sep = ',') {   const entries const= entries=ObjectObject.entries(obj);   return entries.reduce((str, [p, val], counter) => {   if (counter < entries.length - 1) {   return `${str}${p}=$${k_sep}${val}${sepv_sep}`;   } else {   return  `${str}${p}=$${k_sep}${val}`;   }   }, '');   } 
function/** * Object to string * @param {*} obj * @param {*} k_sep keys separator * @param {*} v_sep values separator * @returns */ var objectToString = function(obj, sepk_sep = '=', v_sep = ',') { const entries = Object.entries(obj); return entries.reduce((str, [p, val], counter) => { if (counter < entries.length - 1) { return `${str}${p}=$${k_sep}${val}${sepv_sep}`; } else { return `${str}${p}=$${k_sep}${val}`; } }, ''); } console.log( objectToString({ status_code: 200, execute_time: 0.1, ip: '1270.0.0.1' }, ',:', ",")) console.log( objectToString({ status_code: 200, execute_time: 0.1, ip: '1270.0.0.1' }, '=', ","))
function objectToString(obj, sep = ',') {   const entries=Object.entries(obj);   return entries.reduce((str, [p, val], counter) => {   if (counter < entries.length - 1) {   return `${str}${p}=${val}${sep}`;   } else {   return  `${str}${p}=${val}`;   }   }, '');   } 
function objectToString(obj, sep = ',') { const entries = Object.entries(obj); return entries.reduce((str, [p, val], counter) => { if (counter < entries.length - 1) { return `${str}${p}=${val}${sep}`; } else { return `${str}${p}=${val}`; } }, ''); } console.log( objectToString({ status_code: 200, execute_time: 0.1, ip: '1270.0.0.1' }, ','))
/** * Object to string * @param {*} obj * @param {*} k_sep keys separator * @param {*} v_sep values separator * @returns */  var objectToString: function (obj, k_sep = '=', v_sep = ',') { const entries = Object.entries(obj); return entries.reduce((str, [p, val], counter) => { if (counter < entries.length - 1) { return `${str}${p}${k_sep}${val}${v_sep}`; } else { return `${str}${p}${k_sep}${val}`; } }, ''); } 
/** * Object to string * @param {*} obj * @param {*} k_sep keys separator * @param {*} v_sep values separator * @returns */ var objectToString = function(obj, k_sep = '=', v_sep = ',') { const entries = Object.entries(obj); return entries.reduce((str, [p, val], counter) => { if (counter < entries.length - 1) { return `${str}${p}${k_sep}${val}${v_sep}`; } else { return `${str}${p}${k_sep}${val}`; } }, ''); } console.log( objectToString({ status_code: 200, execute_time: 0.1, ip: '1270.0.0.1' }, ':', ",")) console.log( objectToString({ status_code: 200, execute_time: 0.1, ip: '1270.0.0.1' }, '=', ","))
Source Link
loretoparisi
  • 16.3k
  • 12
  • 113
  • 159

A modified approach using reduce that let to change the separator sep between (key,value) tuple while checking last item:

function objectToString(obj, sep = ',') { const entries=Object.entries(obj); return entries.reduce((str, [p, val], counter) => { if (counter < entries.length - 1) { return `${str}${p}=${val}${sep}`; } else { return `${str}${p}=${val}`; } }, ''); } 

function objectToString(obj, sep = ',') { const entries = Object.entries(obj); return entries.reduce((str, [p, val], counter) => { if (counter < entries.length - 1) { return `${str}${p}=${val}${sep}`; } else { return `${str}${p}=${val}`; } }, ''); } console.log( objectToString({ status_code: 200, execute_time: 0.1, ip: '1270.0.0.1' }, ','))