8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CoffeeScript: 配列の差分(diff)を取る一行の関数

Last updated at Posted at 2014-04-09
list1 = ["green", "red", "blue", "red"]
list2 = ["green", "yellow", "red"]

# 独立した関数を定義する場合
diff = (list1, list2)-> (value for value in list1 when list2.indexOf(value) is -1)

# prototypeを拡張する場合
Array::diff = (array)-> (value for value in this when array.indexOf(value) is -1)

console.log diff(list1, list2)
console.log list1.diff(list2)

IE8以下対応は Array.prototype.indexOf を定義しないとだめ http://qiita.com/suin/items/f08af948279f87d84865#1-1

Array::indexOf or= (item) ->
    for x, i in this
        return i if x is item
    return -1
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?