admin管理员组文章数量:1240607
In Visual studio code, When I am using chart.js in my app, prettier always put a ma at the end of the last data of the object 'label'. I think, it's create a bug which unable to show my chart on my browser. it show blank. Code is given bellow.
let massPopChart2 = new Chart(myChart2, {
type: "bar", // bar, horizontalBar, pie, line, doughnut, radar, polarArea
data: {
labels: [
"1st Day",
"2nd Day",
"3rd Day",
"4th Day",
"5th Day",
"6th Day",
"7th Day",
],
},
});
In Visual studio code, When I am using chart.js in my app, prettier always put a ma at the end of the last data of the object 'label'. I think, it's create a bug which unable to show my chart on my browser. it show blank. Code is given bellow.
let massPopChart2 = new Chart(myChart2, {
type: "bar", // bar, horizontalBar, pie, line, doughnut, radar, polarArea
data: {
labels: [
"1st Day",
"2nd Day",
"3rd Day",
"4th Day",
"5th Day",
"6th Day",
"7th Day",
],
},
});
can anyone help me figure out why this happening?
Share
Improve this question
asked Jun 1, 2021 at 17:03
shahadat3669shahadat3669
1151 gold badge2 silver badges6 bronze badges
1
-
2
You can change it by setting
'trailingComma': 'none'
in your prettier config. – nsevens Commented Jun 1, 2021 at 17:07
3 Answers
Reset to default 10JavaScript has allowed trailing mas in array literals since the beginning, and later added them to object literals (ECMAScript 5) and most recently (ECMAScript 2017) to function parameters.
This is a relatively new change in syntax, but the basic idea is that by putting a ma on each line allows for:
- Easier to add an item or re-order items. Before you always had to check the trailing ma and make sure it was present or removed depending on location.
- Removes the need to have one line item be special because it lacks the
,
. - Allows for cleaner Git diffs.
You can read up on the full documentation if you like - it goes into further detail: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Trailing_mas
As far as the issue with your chart not displaying, unless you are using a very old browser, a trailing ma should not cause an error/information to not display.
You need to update the configuration of prettier
extension.
There are two ways. Below one is mostly used.
Create a
.prettierrc
file at the root of your project and specifying the below configuration.{ "trailingComma": "es5" }
In order to honor the configuration make sure to enable the below setting in vs code configuration.
"prettier.requireConfig": true
Prettier adds those mas at the end just because if you wanna add another data after that you don't need to type a ma. it does the same for semicolons(;).
you got the error because you haven't provided datasets.
data takes an object which contains labels & datasets values.
{/* <canvas id="myChart" width="400" height="400"></canvas> */}
// var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['1','2'],
datasets: [
{
label: '1st',
data: '120',
borderColor: Utils.CHART_COLORS.red,
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.red, 0.5),
},
{
label: '2',
data: '240',
borderColor: Utils.CHART_COLORS.red,
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.blue, 0.5),
}
]
},
// options: {
// indexAxis: 'y',
// elements: {
// bar: {
// borderWidth: 2,
// }
// },
// responsive: true,
// plugins: {
// legend: {
// position: 'right',
// },
// title: {
// display: true,
// text: 'Chart.js Horizontal Bar Chart'
// }
// }
// },
// };
you can know more about it on official docs https://www.chartjs/docs/latest/charts/bar.html
本文标签: javascriptWhy prettier put a comma 3939 at the last element of the objectStack Overflow
版权声明:本文标题:javascript - Why prettier put a comma ',' at the last element of the object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740084051a2223641.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论