How clean the javascript object by replace() in javascript Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How clean the javascript object by replace() in javascript without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a object in this format

{"paymethod_id":1,"business_id":76,"delivery_type":"1","driver_tip":0,"delivery_zone_id":6569,"delivery_datetime":null,"location":{"lat":18.7675049,"lng":-103.1445221},"deliveryOptionmodal":{"id":3,"value":"Sin contacto/Dejar orden en la puerta","$$hashKey":"object:272"},"delivery_cost_new":10,"products":{"name":"Product"},"customer_id":35,"customer":"{"id":35,"name":"Hong Kong","middle_name":null,"lastname":"","second_lastname":null,"photo":"https://res.cloudinary.com/ordering2/image/upload/v1551225299/taomauvuhrrowqqp3ncp.png","email":"hongkongpide@gmail.com","cellphone":"4433413248","address":"Coalcomán, Mich., México","location":"{\"lat\":18.7675049,\"lng\":-103.1445221}","internal_number":null,"address_notes":null,"zipcode":null,"map_data":{"library":"google","place_id":"ChIJz6WGrUw-MIQR_jYIoFZ-RPM"},"tag":"home"}","business_name":"Soporte Devy"}

Which is not easily readable is there any way i can clean this object and see like this

business_name: Sport Devy
name: hong kong

I just want to clean the object and convert it into representable form

Answer

You could write a function to transform it into the format you want. I am not sure if you specifically want it as a string or as a different object. I am outputting a string but you could modify this to return an object if that is what you need.

function formatDisplay(obj) {
   const bname = obj.business_name;
   const customer = JSON.parse(obj.customer || "{}");
   const name = customer && customer.name;
   /* If you need object:
   return {
     business_name: bname,
     name: name
   };
   */
   return [
    "business name: "+ bname,
    "name: " + name,
   ].join("n");
};

const data = {"paymethod_id":1,"business_id":76,"delivery_type":"1","driver_tip":0,"delivery_zone_id":6569,"delivery_datetime":null,"location":{"lat":18.7675049,"lng":-103.1445221},"deliveryOptionmodal":{"id":3,"value":"Sin contacto/Dejar orden en la puerta","$$hashKey":"object:272"},"delivery_cost_new":10,"products":{"name":"Product"},"customer_id":35,"customer":"{"id":35,"name":"Hong Kong","middle_name":null,"lastname":"","second_lastname":null,"photo":"https://res.cloudinary.com/ordering2/image/upload/v1551225299/taomauvuhrrowqqp3ncp.png","email":"hongkongpide@gmail.com","cellphone":"4433413248","address":"Coalcomán, Mich., México","location":"{\"lat\":18.7675049,\"lng\":-103.1445221}","internal_number":null,"address_notes":null,"zipcode":null,"map_data":{"library":"google","place_id":"ChIJz6WGrUw-MIQR_jYIoFZ-RPM"},"tag":"home"}","business_name":"Soporte Devy"};

console.log(formatDisplay(data));
We are here to answer your question about How clean the javascript object by replace() in javascript - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji