如何通过 javascript 中的 replace() 清理 javascript 对象

How clean the javascript object by replace() in javascript

我有一个这种格式的对象

{"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"}

这不容易阅读有什么方法可以清理这个对象并看到这个

business_name: Sport Devy
name: hong kong

我只想清理对象并将其转换为可表示形式

const obj = {
  "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"
}

business_name可以直接从对象中读取-obj.business_namecustomer节点需要解析成javascript对象;为此,您可以使用 JSON.parse(obj.customer)

你可以写一个函数把它转换成你想要的格式。我不确定您是特别希望它作为字符串还是作为不同的对象。我正在输出一个字符串,但如果您需要的话,您可以将其修改为 return 一个对象。

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));