小编Ale*_*edt的帖子

未捕获的TypeError:Object.values不是函数JavaScript

我有一个简单的对象,如下所示:

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()

javascript arrays cross-browser object node.js

73
推荐指数
3
解决办法
7万
查看次数

从6迁移到7后,无法比较茉莉花中的枚举

我正在将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)

enums karma-jasmine angular angular6 angular7

5
推荐指数
1
解决办法
520
查看次数

使用Ecto一次插入多行."协议可枚举的#Ecto.Changeset未实现"

我有两张桌子.的表格topics其中has_many tweets.我的表tweets belongs_totopic.

主题架构:

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 ecto phoenix-framework

3
推荐指数
1
解决办法
3753
查看次数

Elixir doctest 对于返回随机值的函数失败

我在 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)

当我运行测试时,我的文档测试失败了。生成的元组列表与我的文档测试中定义的不同。如何为返回随机值的函数编写文档测试?

elixir ex-unit

2
推荐指数
1
解决办法
1002
查看次数

如何使用 Rust 和 web-sys 将 Clamped&lt;Vec&lt;u8&gt;&gt; 转换为 Clamped&lt;&amp;mut [u8]&gt; ?

我正在使用 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 …

rust webassembly wasm-bindgen

2
推荐指数
1
解决办法
726
查看次数