繁体   English   中英

如何替换js对象中的文本?

[英]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.

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