VS代码 - 如何仅从键中删除报价,但值仍然存在

发布于 2025-01-23 03:06:06 字数 385 浏览 3 评论 0原文

在不使用引号的情况下,什么扩展名或快捷方式将格式化JavaScript对象的键?

我在演示文稿中看到了这一点:他们将下面的示例粘贴到VS代码中,然后将其转换为同一文件中的b)中的示例。

注意,并非所有双引号都被

"library" : {
  "books": 123,
  "genres": "all types",
  "hoursOpen": 8,
}

删除

library : {
  books: 123,
  genres: "all types",
  hoursOpen: 8,
}

请 右扩展或弄清楚它是什么。 他们没有运行任何代码,这里有什么快捷方式?谢谢你!

What extension or shortcut will format a JavaScript object's keys without using quotation marks?

I saw this in a presentation: They Pasted the example in A) below into VS Code and then it converted into the example in B) in the same file.

Please note not all the double quotations were removed because on the right hand side (value - "all types") remains but only the keys on the left had the "" removed

Example A) Before

"library" : {
  "books": 123,
  "genres": "all types",
  "hoursOpen": 8,
}

Example B) After

library : {
  books: 123,
  genres: "all types",
  hoursOpen: 8,
}

I couldn't find the right extension or figure out what it is.
They didn't run any code, what shortcut is done here? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-柠檬树下少年和吉他 2025-01-30 03:06:06

2个流行的扩展名,从JS Object Keys


Eslint&正确配置时,Prettier都会从您的属性键中删除引号。以下是两个扩展的链接。下面的链接在左侧是不同的。正确的。左侧是工具主页,右侧是该工具在VS代码市场中的扩展。

扩展名称扩展ID
eslint eslint> dbaeumer.vscode-eSlint
a href =“ https://prettier.io/” rel =“ nofollow noreferrer”> Prettieresbenp.prettier-vscode

应注意的是,并非所有格式化者都从属性中删除了引号。另一个半广泛的格式化 - js-beautify - 没有删除引号的规则从对象的键。

最快&最简单意味着


您正在寻找“插头& play”类型的扩展名。就很少的设置而言,快速开始进行: Prettier 是您最好的选择。 ESLINT需要一定程度的知识,或者需要配置.eslintrc。*文件所花费的时间,以便获得知识。 Prettier将让您格式化代码,并在下载2秒后从属性中删除引号,以及可以很快撰写的配置文件。

  • 步骤1- 下载VS代码的漂亮扩展名,请确保它是下载最多的扩展名。 ID应匹配我上面发布的ID。

  • 步骤2 - 将以下设置添加到您的settings.json配置文件。

任何VS代码settings.json配置文件都将起作用。您可以在项目.vscode目录中使用工作空间示波器文件,或者使用用户划分的settings.json文件配置文件。

  // @file "./.vscode/settings.json"
  {
    // Sets the formatter to format when the file is saved.
    "editor.formatOnSave": false,
    // Sets prettier to format your code
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    
  }

  • 步骤3- 在您正在从事的基本目录(又称根目录)中,添加一个名为.prettierrc的文件。这些文件是大多数Linters的标准配置。格式化。

  • 步骤4- 将以下配置添加到您的新.prettierrc file。

  // @file "$PROJ_ROOTDIR/.prettierrc"
  
  {
    "quoteProps": "as-needed",
    "singleQuote": false,
    "printWidth": 80,
    "trailingComma": "none",
    "tabWidth": 4,
    "semi": true
  }

“ quoteprops”:“ as-neded” 规则将配置您的项目以从JavaScript允许这样做的对象中删除所有引号。在几个情况下, ecma-262 标准需要引用密钥,但它们是远的&介于两者之间。该规则将处理这些案件,因此您不必为此担心。

我将通常配置的其他属性包括在文件中,以便您知道它们在那里。

另请注意,如果您的项目不配置为同时使用,则将ESLINT与Prettier一起使用可能会有问题。

如果您选择了我建议的更漂亮的选项,则需要查看文档,因此您可以在内部学习该工具。尤其是如果您经常写JavaScript。这是 Prettier文档&规则列表

2 Popular Extensions that Remove Quote Marks from JS Object Keys


ESLint & prettier will both remove the quotation marks from your properties keys when configured properly. Below are the links for the two extensions. The links below are different on the left & right. The left side is the tools homepage, and the right side is the Tool's extension in the VS Code Marketplace.

Extension NameExtension ID
ESLintdbaeumer.vscode-eslint
Prettieresbenp.prettier-vscode

It should be noted that not all formatters remove quotation marks from properties. Another semi-popular formatter — JS-Beautify — has NO rule for removing the quotation marks from an object's keys.

The Quickest & Most Simple Means


It sounds like your looking for a "plug & play" type of extension. As far as little setup, and getting go quickly goes: Prettier is your best bet. ESLint requires a certain level of knowledge, or time spent configuring the .eslintrc.* file so that the knowledge is gained. Prettier will have you formatting your code, and removing quotes from properties after a 2 second download, and a configuration file that can be authored rather quickly.

  • STEP 1 - Download The Prettier Extension for VS Code, make sure it is the one that has the most downloads. The ID should match the ID I posted above.

  • STEP 2 - Add the following settings to your settings.json configuration file.

Any of the VS Code settings.json configuration files will work. You can use the workspace scoped file in your projects .vscode directory, or the user-scoped settings.json file configuration file.

  // @file "./.vscode/settings.json"
  {
    // Sets the formatter to format when the file is saved.
    "editor.formatOnSave": false,
    // Sets prettier to format your code
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    
  }

  • STEP 3 - In the base directory (aka Root-directory) of which ever project you are working on, add a file named .prettierrc. These files are standard for most linters & formatters.

  • STEP 4 - Add the following configuration to your new .prettierrc file.

  // @file "$PROJ_ROOTDIR/.prettierrc"
  
  {
    "quoteProps": "as-needed",
    "singleQuote": false,
    "printWidth": 80,
    "trailingComma": "none",
    "tabWidth": 4,
    "semi": true
  }

The "quoteProps": "as-needed" rule will configure your project to remove all quotation marks from objects where JavaScript permits doing so. There are a couple cases where the ECMA-262 standard requires keys to be quoted, but they are far & few in between. This rule will handle those cases so you don't need to worry about that.

I included the other properties I typically configure into the file so that you are aware that they are there.

Also note that using ESLint with Prettier can be problematic if your project is not configured to use both.

If you opt for the prettier option I suggested, you will want to view the documentation, so you can learn the tool inside & out, especially if you will be writing JavaScript often. Here is the link to the Prettier Documentation & Rules List

一页 2025-01-30 03:06:06

不要重新发明轮子...

const jsonObj = {"foo": "bar", "key": "value"}
const javascriptObj = JSON.parse(obj)


console.log(javascriptObj) // { foo: "bar", key: "value" }
console.log(JSON.parse(jsonObj) // { foo: "bar", key: "value" }

很少有人要注意...首先,如果密钥不是有效的JavaScript标识符,它将在其中扔引号。.aka,如果它以数字等开头等...

另一件事..另一件事.. 。因此,如果不是完全的字符串 - >您可以先将其串起。

JSON.parse(JSON.stringify(obj))

Dont reinvent the wheel...

const jsonObj = {"foo": "bar", "key": "value"}
const javascriptObj = JSON.parse(obj)


console.log(javascriptObj) // { foo: "bar", key: "value" }
console.log(JSON.parse(jsonObj) // { foo: "bar", key: "value" }

few things to note... First of all if the key is not a valid javascript identifier it will throw the quotes in there.. aka if it starts with a number etc...

another thing... it should be in JSON string notation if you want it to be parseable. so if it's not fully a string -> you can stringify it first.

JSON.parse(JSON.stringify(obj))
囚我心虐我身 2025-01-30 03:06:06

一种选择是使用 eslint 和eslint扩展,启用 quote-props 规则,然后右键单击带有一个带有一个带有的段不必要的报价和自动框架所有问题:

”在此处输入图像描述”

One option is to use ESLint and the ESLint extension, enable the quote-props rule, then right click on one of the segments with unnecessary quotes and auto-fix all problems:

enter image description here

单调的奢华 2025-01-30 03:06:06

使用Prettier Extension

在安装漂亮的扩展名,打开设置(齿轮图标)左下角,搜索“保存格式”,如下所示,该框应在您保存文件时可以格式化

代码。

​,默认情况下,Prettier删除了JS对象的键上的引号,因此您应该出于

某种原因,您想覆盖此默认行为并选择在键上有报价 ,然后创建一个名为.prettierrc.json的文件,在最大的项目级别(通常将与src文件夹中的级别相同)。 quoteprops的条目,并将其设置为保存

{
  "semi": true,
  "singleQuote": true,
  "endOfLine": "lf",
  "tabWidth": 2,
  "quoteProps": "preserve" // this will add quotes. "as-needed" is the default that will remove the quotes
}

Use prettier extension

In VsCode once you install the prettier extension, Open settings (gear icon on left bottom corner, search for "Format on save", and check the box as shown below. This should enable prettier to format the code whenever you save the file.

enter image description here

And to answer your question, by default the prettier removes the quotes on the keys of JS Objects so you should be good.

For some reason, if you want to override this default behaviour and choose to have quotes on keys, then create a file called .prettierrc.json in outer-most level of project (generally will be at same level as src folder). In this file, you can add an entry for quoteProps and set it to preserve

{
  "semi": true,
  "singleQuote": true,
  "endOfLine": "lf",
  "tabWidth": 2,
  "quoteProps": "preserve" // this will add quotes. "as-needed" is the default that will remove the quotes
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文