简体   繁体   English

如何在 JavaScript/Typescript 中轻松地将方括号表示法 (abc['prop']) 替换为点表示法 (abc.prop)?

[英]How to replace square bracket notation (abc['prop']) to dot notation (abc.prop) in JavaScript/Typescript easily?

I am editing some code which has been written with a lot of unnecessary square bracket notations.我正在编辑一些用很多不必要的方括号表示法编写的代码。 For better understanding of the code, I want to convert stuff like abc[3]['prop']["subprop"] to abc[3].prop.subprop .为了更好地理解代码,我想将abc[3]['prop']["subprop"]类的东西转换为abc[3].prop.subprop

I can do this almost perfectly by doing text replacement in VSCode multiple times using regex ([\\w_$][\\w$_\\d]*(?:(?:\\[\\d+\\])+)?)\\[["']([\\w_$][\\w$_\\d]*)["']\\] to $1.$2 .通过使用正则表达式在 VSCode 中多次进行文本替换,我几乎可以完美地做到这一点([\\w_$][\\w$_\\d]*(?:(?:\\[\\d+\\])+)?)\\[["']([\\w_$][\\w$_\\d]*)["']\\]$1.$2

But it would also be nice to convert { 'prop': value1, "prop2": value2 } to { prop: value1, prop2: value2 } .但是将{ 'prop': value1, "prop2": value2 }{ prop: value1, prop2: value2 }

Is there any way to accomplish both goals, perhaps a prettier/beautifier/formatter extension or utility?有没有办法实现这两个目标,也许是更漂亮/美化/格式化的扩展或实用程序?

The dot-notation rule for ESLint with the --fix option can automatically correct it.带有--fix选项的--fix dot-notation规则可以自动更正它。

quote-props (which can also be auto-fixed) can also remove unnecessary quotes around properties, thanks Bravo. quote-props (也可以自动修复)还可以删除属性周围不必要的引号,感谢 Bravo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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