替换JSON URL数据中的斜杠

时间:2018-05-02 09:54:47

标签: json node.js express formatting

我想在 productImage 路径中将“\\”替换为“/”,我的代码如下所示替换方法但是没有工作,等你的帮助谢谢你

const response = {
    count: docs.length,
    products: docs.map(doc => {
        return {
            name: doc.name,
            price: doc.price,
            productImage: doc.productImage,
            _id: doc._id,
            request: {
                type: "GET",
                url: url + "/products/" + doc._id
            }
        };
    })
};

我的JSON输出

{
    "name": " Sample Product",
    "price": 45,
    "productImage": "uploads\\2018-05-02T09-12-59.937Zahmet.jpg",
    "_id": "5ae9811be170eb0a78500088",
    "request": {
        "type": "GET",
        "url": "http://localhost:3000/products/5ae9811be170eb0a78500088"
    }
},

1 个答案:

答案 0 :(得分:1)

您可以按\\拆分该值,您将获得一个数组,并使用/

加入该数组



var obj = {
    "name": " Sample Product",
    "price": 45,
    "productImage": "uploads\\2018-05-02T09-12-59.937Zahmet.jpg",
    "_id": "5ae9811be170eb0a78500088",
    "request": {
        "type": "GET",
        "url": "http://localhost:3000/products/5ae9811be170eb0a78500088"
    }
}

obj.productImage = obj.productImage.split("\\").join("/");


console.log(obj)