解构赋值可以用于影响 CoffeeScript 中的投影吗?
我在理解 CoffeeScript 中的解构赋值时遇到一些困难。 文档包含几个示例,这些示例似乎暗示在赋值期间重命名对象可以用于投影(即映射、平移、变换)源对象。
我正在尝试将 a = [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ]
投影到 b = [ { x: 1 }, { x: 2 }]
。我尝试了以下方法但没有成功;我显然误解了一些东西。谁能解释这是否可能?
我糟糕的尝试不返回 [ { x: 1 }, { x: 2 } ]
a = [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ]
# Huh? This returns 1.
x = [ { Id } ] = a
# Boo! This returns [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ]
y = [ { x: Id } ] = a
# Boo! This returns [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ]
z = [ { Id: x } ] = a
CoffeeScript 的并行赋值示例
theBait = 1000
theSwitch = 0
[theBait, theSwitch] = [theSwitch, theBait]
我理解这个示例暗示变量可以重命名,在本例中用于执行交换。
CoffeeScript 的任意嵌套示例
futurists =
sculptor: "Umberto Boccioni"
painter: "Vladimir Burliuk"
poet:
name: "F.T. Marinetti"
address: [
"Via Roma 42R"
"Bellagio, Italy 22021"
]
{poet: {name, address: [street, city]}} = futurists
我将此示例理解为从任意对象定义属性选择,其中包括将数组的元素分配给变量。
更新:使用 thejh 的解决方案来展平嵌套对象数组
a = [
{ Id: 0, Name: { First: 'George', Last: 'Clinton' } },
{ Id: 1, Name: { First: 'Bill', Last: 'Bush' } },
]
# The old way I was doing it.
old_way = _.map a, x ->
{ Id: id, Name: { First: first, Last: last } } = x
{ id, first, last }
# Using thejh's solution...
new_way = ({id, first, last} for {Id: id, Name: {First: first, Last: last}} in a)
console.log new_way
I'm having some trouble understanding destructuring assignment in CoffeeScript. The documentation contains a couple of examples which together seem to imply that renaming objects during assignment can be used to project (i.e. map, translate, transform) a source object.
I am trying to project a = [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ]
into b = [ { x: 1 }, { x: 2 } ]
. I've tried the following without success; I've clearly misunderstood something. Can anyone explain whether this is possible?
My Poor Attempts That Don't Return [ { x: 1 }, { x: 2 } ]
a = [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ]
# Huh? This returns 1.
x = [ { Id } ] = a
# Boo! This returns [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ]
y = [ { x: Id } ] = a
# Boo! This returns [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ]
z = [ { Id: x } ] = a
CoffeeScript's Parallel Assignment Example
theBait = 1000
theSwitch = 0
[theBait, theSwitch] = [theSwitch, theBait]
I understand this example as implying that variables can be renamed which in this case is used to perform a swap.
CoffeeScript's Arbitrary Nesting Example
futurists =
sculptor: "Umberto Boccioni"
painter: "Vladimir Burliuk"
poet:
name: "F.T. Marinetti"
address: [
"Via Roma 42R"
"Bellagio, Italy 22021"
]
{poet: {name, address: [street, city]}} = futurists
I understand this example as defining a selection of properties from an arbitrary object which includes assigning the elements of an array to variables.
Update: Using thejh's Solution to Flatten an Array of Nested Objects
a = [
{ Id: 0, Name: { First: 'George', Last: 'Clinton' } },
{ Id: 1, Name: { First: 'Bill', Last: 'Bush' } },
]
# The old way I was doing it.
old_way = _.map a, x ->
{ Id: id, Name: { First: first, Last: last } } = x
{ id, first, last }
# Using thejh's solution...
new_way = ({id, first, last} for {Id: id, Name: {First: first, Last: last}} in a)
console.log new_way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
b = ({x} for {Id: x} in a)
有效:b = ({x} for {Id: x} in a)
works:CoffeeScript Cookbook 解决了与您完全相同的问题 - 解决方案是 地图:
或 列表理解:
您可以检查 这个小提琴在 CoffeScript 主页上在线(使用 console.info 显示结果)。
编辑:
要使其具有破坏性,只需将映射变量
a
分配给自身:CoffeeScript Cookbook solves exactly the same problem as yours - solution is map:
or list comprehension:
You can check this fiddle online on CoffeScript homepage (uses console.info to show results).
EDIT:
To make it destrutive just assign mapped variable
a
to itself: