简体   繁体   中英

How do I replace text within a js object?

Here is my attempt:

  locationVar = {"xpath":".//*[text()=\"" + uniqueMenuItems[b] + "\"]//following::*[@class=\"productControllers custom-product-ctrls\"][1]/div/div/select"};
  optionLocator = locationVar.replace("select", "select/option");

But this throws error locationVar.replace is not a function.

How do I tell js to do the replace on the text within the object? .toString().replace works to change it to a string then replace it, however I need the object to remain a js object after the replacement.

Is there a .replace() method that works on objects?

As said in the comments: Use replace on the object property, not the object itself.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM