admin管理员组文章数量:1326320
I am trying to print variables in an array using back ticks and dollar sign, but somehow it doesn't retrieve data from the array.
var buz = {
fog: 'stack',
snow: 'white'
};
for (var key in buz) {
if (buz.hasOwnProperty(key)) {
console.log(`this is fog {$key} for sure. Value: {$buz[key]}`);
} else {
console.log(key); // toString or something else
}
}
I am trying to print variables in an array using back ticks and dollar sign, but somehow it doesn't retrieve data from the array.
var buz = {
fog: 'stack',
snow: 'white'
};
for (var key in buz) {
if (buz.hasOwnProperty(key)) {
console.log(`this is fog {$key} for sure. Value: {$buz[key]}`);
} else {
console.log(key); // toString or something else
}
}
this prints:
this is fog {$key} for sure. Value: {$buz[key]}
this is fog {$key} for sure. Value: {$buz[key]}
How can I print:
this is fog fog for sure. Value: stack
this is fog snow for sure. Value: white
?
Share Improve this question edited Oct 18, 2018 at 15:50 Barry 3,3287 gold badges25 silver badges43 bronze badges asked Dec 19, 2017 at 16:34 bombom 831 silver badge9 bronze badges 1- ah thank you all so much! – bom Commented Dec 19, 2017 at 16:39
4 Answers
Reset to default 8You need ${expression}
. For more info, see https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Template_literals
var buz = {
fog: 'stack',
snow: 'white'
};
for (var key in buz) {
if (buz.hasOwnProperty(key)) {
console.log(`this is fog ${key} for sure. Value: ${buz[key]}`);
} else {
console.log(key); // toString or something else
}
}
Change {$key}
to ${key}
and {$buz[key]}
to ${buz[key]}
like following:
console.log(`this is fog ${key} for sure. Value: ${buz[key]}`);
I think you're looking for:
console.log(`this is fog ${key} for sure. Value: ${buz[key]}`);
you are doing it wrong
it should be like this
abc ${something}
本文标签: javascriptprint variables using consolelog() with back ticks and dollar signStack Overflow
版权声明:本文标题:javascript - print variables using console.log() with back ticks and dollar sign - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742203073a2432318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论