1

アプリの鳥とやりたいとしましょうwherepluck

word_typeまず、名詞であるすべてのを見つけたいと思います。

次に、word属性を取得して、最終結果が次のようになるようにします。

名詞であるすべての単語。

["Lion", "Capybara", "Cat"]

私が試してみると:

@words = new Project.Collections.WordsCollection()
@words.where(word_type: "noun").pluck("word_type")

戻ります:

this.words.where({word_type: "noun"}).pluck is not a function
4

1 に答える 1

3

問題はwhere、モデルの配列を返し、その配列にアンダースコアメソッドが混在していないことです。あなたはpluck(実際には特定のタイプのmap)を展開することができます:

_(@words.where(work_type: 'noun')).map (m) -> m.attributes.word_type

またはアンダースコアミックスインを使用します:

@chain()
    .filter((m) -> m.attributes.word_type == 'noun')
    .map((m) -> m.attributes.word_type)
    .value()

または、同じものをフィルタリングして抽出しているので、一致をカウントし、次の後に配列を構築します。

n = @reduce(
    ((n, m) -> n += (if m.attributes.word_type == 'noun' then 1 else 0)),
    0
)
matches = ('noun' for i in [0 ... n])
于 2012-07-04T01:32:01.830 に答える