首先,我使用Cheerio进行DOM访问并使用Node.js进行解析.美好时光.
继承人的情况:
我有一个功能,我需要创建一个对象.该对象使用其键和值的变量,然后返回该单个对象.例:
stuff = function (thing, callback) {
var inputs = $('div.quantity > input').map(function(){
var key = this.attr('name')
, value = this.attr('value');
return { key : value }
})
callback(null, inputs);
}
Run Code Online (Sandbox Code Playgroud)
它输出这个:
[ { key: '1' }, { key: '1' } ]
Run Code Online (Sandbox Code Playgroud)
(.map()返回一个对象数组fyi)
我需要key实际上是来自的字符串this.attr('name').
什么是在Javascript中将字符串指定为键的最佳方法,考虑到我正在尝试做什么?
在JavaScript中,反引用†似乎与单引号相同.例如,我可以使用反引号来定义这样的字符串:
var s = `abc`;
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使反引号的行为实际上与单引号的行为不同?
†请注意,在程序员中,"反引号"是更常被称为重音符号的名称.程序员有时也使用替代名称 "反引号"和"反向".此外,在Stack Overflow和其他地方,"反击"的其他常见拼写是"后退"和"后退".
javascript backticks template-strings single-quotes backquote
我想使用点表示法更新 Cloud Firestore 中嵌套对象中的字段。但点符号引用的那些字段是变量。
我怎样才能做到这一点?
文档// To update age and favorite color:
db.collection("users").doc("frank").update({
"age": 13,
"favorites.color": "Red"
})
Run Code Online (Sandbox Code Playgroud)
但是如果我需要创建favorites.color一个变量怎么办?
const attributes = [ 'jobs', 'schools', 'favorites', ];
const values = [ 'food', 'song', 'color', ];
const variableData = [attributes[2]].[values[2]]; // 'favorites.color'
Run Code Online (Sandbox Code Playgroud)
我可不可以做:
db.collection("users").doc("frank").update({
"age": 13,
"[attributes[2]].[values[2]]": "Red"
})
Run Code Online (Sandbox Code Playgroud)
确切地说,这会如何运作?
编辑:
我试过:
db.collection("users").doc("frank").update({
"age": 13,
`${[attributes[2]]}.${[values[2]]}`: "Red"
})
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
解析错误:意外的标记
尝试时也会出现同样的错误
`"${[attributes[2]]}.${[values[2]]}"`: "Red"
Run Code Online (Sandbox Code Playgroud)
(认为 Firebase 需要引号来解析语法。)
我正在寻找一种简单的语法来允许我创建一个具有一个属性的新对象并设置该属性的值。这是一个例子。我希望能够在一条线上完成所有事情。
let parent = 'jackets',
let responses = [{'color':'red', 'size':'medium'}]
let tmp = {}
tmp[parent] = responses
return tmp
Run Code Online (Sandbox Code Playgroud)
应该记录:
{
"jackets": [
{
'color':'red',
'size':'medium'
}
]
}
Run Code Online (Sandbox Code Playgroud)
我以为我曾经看到这在 es-spec 中是可能的,但它不适用于 babel-2015 预设。
let jackets = {`${parent}`: responses}
Run Code Online (Sandbox Code Playgroud) 我创建一个如下所示的对象,
var s = {
`1234`:`string`
}
Run Code Online (Sandbox Code Playgroud)
但是,它会抛出以下错误,
Uncaught SyntaxError: Unexpected template string
Run Code Online (Sandbox Code Playgroud)
我怎样才能创建这样的元素?
在视图中我需要生成以下类:
<div class="comp comp--lock comp--red">Foo</div>
Run Code Online (Sandbox Code Playgroud)
和lock基于red状态,其中可能有以下颜色值:
comp--red、comp--yellow、comp--blue和许多其他可能的颜色到目前为止,我一直在使用计算方法根据数据连接类名:
getCompClassName(){
return `comp ${this.isLock ? 'comp--lock' : ''} comp--${this.color}`
}
Run Code Online (Sandbox Code Playgroud)
查看 Vuejs 文档,我发现应该以v-bind:class更好的方式解决这个问题,我遇到的问题是如何解决插值color,因为我需要声明所有可能的颜色。
data: {
classObject: {
'comp--lock': this.isLock,
'comp--red': this.color === 'red',
'comp--blue': this.color === 'blue',
'comp--yellow': this.color === 'yellow'
}
}
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以使用v-bind:class更好的扩展来解决这个问题,而不必列出所有可能性,或者我应该使用计算方法来插入类名?
为什么对象属性的反引号语法无效?例如,这是有效的:
\nconst test = {\n "test1": "test2"\n}\nRun Code Online (Sandbox Code Playgroud)\n那么为什么 \xe2\x80\x99 不能有反引号呢?
\nconst test = {\n `test1`: "test2"\n}\n//throws unexpected token error\nRun Code Online (Sandbox Code Playgroud)\n如果可以像这样使用反引号,那将非常有帮助,但我们可以\xe2\x80\x99t。是否有一个简单的替代方案,或者这可能在某些浏览器中有效?
\n我可以创建一个将键值作为模板字符串传递的对象吗?
const key = 'my-key', value = 'my-value'
const obj = {
`${key}`: value
}
Run Code Online (Sandbox Code Playgroud)
有替代方法吗?
如果我use strict用反引号/模板文字括起来,“使用严格”不会按预期工作。你能分享一下背后的原因吗?是否有任何类似的异常语句模板文字无法按预期工作?
`use strict`;
x = 3.14; // Ideally it should cause an error (as x is not defined).
alert(x);Run Code Online (Sandbox Code Playgroud)
`use strict`; // if we enclose in single quotes or double quotes
x = 3.14; // Ideally it should cause an error (as x is not defined).
alert(x);
Run Code Online (Sandbox Code Playgroud) javascript ×7
backquote ×1
backticks ×1
ecmascript-6 ×1
firebase ×1
jquery ×1
json ×1
object ×1
reactjs ×1
use-strict ×1
vue.js ×1