我有一个简单的对象,如下所示:
var countries = {
"Argentina":1,
"Canada":2,
"Egypt":1,
};
Run Code Online (Sandbox Code Playgroud)
我需要创建两个数组.第一个数组是来自对象的所有键的数组.我创建了这个数组:
var labels = Object.keys(countries);
Run Code Online (Sandbox Code Playgroud)
这很好用.我获得了一系列国家.现在当我尝试从值创建数组时...
var labels = Object.values(countries);
Run Code Online (Sandbox Code Playgroud)
我收到此错误: Uncaught TypeError: Object.values is not a function JavaScript
我不知道我做错了什么.我console.log countries
在声明之前labels
和之后的console.log 并且对象保持不变.我该如何正确使用Object.values()
?
我正在将Angular应用程序从v6迁移到v7。除了对枚举进行比较的任何测试以外,其他一切都很好。当我运行测试时,会遇到很多关于枚举的错误,例如
ERROR in src/.../some-thing.component.spec.ts: error TS2345: Argument of type 'PlanDuration.SixMonths' is not assignable to parameter of type 'Expected<PlanDuration.TwelveMonths>'.
Run Code Online (Sandbox Code Playgroud)
运行测试的示例如下所示:
export enum PlanDuration {
SixMonths,
TwelveMonths
}
...
it('should toggle plan duration to six months if the event source id is the toggle duration and the event is not checked', () => {
component.selectedPlanDuration = PlanDuration.TwelveMonths;
component.handleToggle(event);
expect(component.selectedPlanDuration).toBe(PlanDuration.SixMonths); // Tests cannot run because of errors here
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我将枚举值转换为数字,则测试将完美运行!像这样在任何地方更新我的规格都不太理想:
expect(component.selectedPlanDuration).toBe(<number> PlanDuration.SixMonths);
Run Code Online (Sandbox Code Playgroud)
我不确定我是否错过了一些东西package.json
。我已经将一个新的angular 7项目与自己的项目进行了比较,并且它们之间的angular core,打字稿,茉莉花和业力版本相同。
如何获得测试以正确比较枚举?下面是我的package.json
"dependencies": {
"@angular/animations": "~7.2.0", …
Run Code Online (Sandbox Code Playgroud) 我有两张桌子.的表格topics
其中has_many
tweets
.我的表tweets
belongs_to
一topic
.
主题架构:
defmodule Sentiment.Topic do
use Sentiment.Web, :model
schema "topics" do
field :title, :string
has_many :tweets, Sentiment.Tweet
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title])
|> validate_required([:title])
end
end
Run Code Online (Sandbox Code Playgroud)
推文架构:
defmodule Sentiment.Tweet do
use Sentiment.Web, :model
schema "tweets" do
field :message, :string
belongs_to :topic, Sentiment.Topic
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, …
Run Code Online (Sandbox Code Playgroud) 我在 Elixir 中有一个函数,可以在列表中生成三个随机 RGB 元组。
defmodule Color do
@doc """
Create three random r,g,b colors as a list of three tuples
## Examples
iex> colors = Color.pick_color()
iex> colors
[{207, 127, 117}, {219, 121, 237}, {109, 101, 206}]
"""
def pick_color() do
color = Enum.map((0..2), fn(x)->
r = Enum.random(0..255)
g = Enum.random(0..255)
b = Enum.random(0..255)
{r, g, b}
end)
end
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我的文档测试失败了。生成的元组列表与我的文档测试中定义的不同。如何为返回随机值的函数编写文档测试?
我正在使用 Rust 和 WebAssembly 操作像素数据,并且正在努力使用操作的像素创建新的 ImageData。
当我获取我的ImageData
数据时,它返回一个Clamped<Vec<u8>>
fn get_buffer_image_data(&self) -> Clamped<Vec<u8>> {
let image_data = match self.buffer_ctx.get_image_data(0.0, 0.0, 640.0, 480.0) {
Ok(d) => d,
Err(_err) => panic!("failed to fetch buffer image data")
};
image_data.data()
}
Run Code Online (Sandbox Code Playgroud)
我在另一个函数中操作像素数据,然后尝试使用这些操作的像素创建新的 ImageData。问题是我只能用Clamped<&mut [u8]>创建新的 ImageData
fn create_image_data(&self, data: Clamped<Vec<u8>>) {
let imageData = ImageData::new_with_u8_clamped_array_and_sh(data, 640, 480);
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到的错误是:
mismatched types
expected `&mut [u8]`, found struct `std::vec::Vec`
note: expected struct `wasm_bindgen::Clamped<&mut [u8]>`
found struct `wasm_bindgen::Clamped<std::vec::Vec<u8>>`
Run Code Online (Sandbox Code Playgroud)
我想我需要将一种类型转换为另一种类型。如何高效转换?我已经尝试这个有一段时间了,但我被困住了。我唯一的解决方案是将整个 Uint8ClampedArray 从我的 JS 发送到 wasm 。这是您可以使用的代码示例。请注意,如果您克隆此存储库,请查看分支problem …
elixir ×2
angular ×1
angular6 ×1
angular7 ×1
arrays ×1
ecto ×1
enums ×1
ex-unit ×1
javascript ×1
node.js ×1
object ×1
rust ×1
wasm-bindgen ×1
webassembly ×1