2015-12-15 132 views
-4

我需要刪除雙引號特定鍵值報價:JavaScript對象刪除特定鍵值

upload: { 
    name: "upload", 
    url: "/upload", 
    controller: "invoicesCtrl", 
    templateUrl: "./views/invoices.html", 
    resolve: "resolveUrl('./compiled/invoices_app.js')" 
    }, 
    invoices: { 
    name: "invoices", 
    url: "/invoices", 
    controller: "invoicesCtrl", 
    templateUrl: "/invoices/views/invoices.html", 
    resolve: "resolveUrl('/invoices/app/js/compiled/invoices_app.js')" 
    } 

只是爲了解決鍵值,其他必須是字符串。有沒有這個replace函數的任何正則表達式?預期的結果:

upload: { 
    name: "upload", 
    url: "/upload", 
    controller: "invoicesCtrl", 
    templateUrl: "./views/invoices.html", 
    resolve: resolveUrl('./compiled/invoices_app.js') 
    }, 
    invoices: { 
    name: "invoices", 
    url: "/invoices", 
    controller: "invoicesCtrl", 
    templateUrl: "/invoices/views/invoices.html", 
    resolve: resolveUrl('/invoices/app/js/compiled/invoices_app.js') 
    } 
+0

假設你正在使用JavaScript,可能重複:http://stackoverflow.com/questions/19156148/i-want-to-remove-double-quotes-from-a -串。 –

+0

@SeanPerkins那不是我所期望的 – AndreFontaine

+0

如果我正確理解你的問題,你想爲'resolve'的當前值運行'eval'的結果賦予'resolve'嗎? – ndn

回答

3

當你說去除雙引號和唯一的雙quoes在resolve是字符串字面終結,你說你想在某個時候被稱爲所謂的resolveUrl的JavaScript函數,那resolve應該是那個調用的結果?

在這種情況下,你可以這樣做:

// iterate through all properties of myObject, assuming it is a flat 
// object containing upload and invoices from your code 
Object.keys(myObject).forEach(function(key) { 

    if(myObject[key].hasOwnProperty('resolve')) 
     myObject[key].resolve = eval(myObject[key].resolve); 

}); 

如果你知道它永遠是一個resolveUrl調用,它會更安全,只是讓原有的屬性包含字符串值 - 例如。 resolve: '/invoices/app/js/compiled/invoices_app.js' - 然後手動調用resolveUrl

if(myObject[key].hasOwnProperty('resolve')) 
    myObject[key].resolve = resolveUrl(myObject[key].resolve);