[英]How do I replace text within a js object?
這是我的嘗試:
locationVar = {"xpath":".//*[text()=\"" + uniqueMenuItems[b] + "\"]//following::*[@class=\"productControllers custom-product-ctrls\"][1]/div/div/select"};
optionLocator = locationVar.replace("select", "select/option");
但這會引發錯誤locationVar.replace不是一個函數。
如何告訴js對對象中的文本進行替換? .toString()。replace可以將其更改為字符串,然后替換它,但是我需要該對象在替換后保持為js對象。
是否有適用於對象的.replace()方法?
如評論中所述:在對象屬性上使用replace
,而不是對象本身。
var uniqueMenuItems = {a: 'foo', b: 'bar'};
var locationVar = {
xpath: `.//*[text()="${uniqueMenuItems.b}"]//following::*[@class="productControllers custom-product-ctrls"][1]/div/div/select`
};
var optionLocator = {
xpath: locationVar.xpath.replace("select", "select/option")
};
console.log(optionLocator.xpath); // logs: .//*[text()="bar"]//following::*[@class="productControllers custom-product-ctrls"][1]/div/div/select/option
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.