9

I have two arrays:

array1 = ["hello","two","three"]
array2 = ["hello"]

I want to check if array2 contains 1 or more array1 words.

How can I do that using Coffeescript?

6 Answers 6

12

Found a way to check for the intersection between two arrays using this CoffeeScript chapter. CoffeeScript seems pretty awesome looking at this.

If the array resulting after the intersection of the elements contains at least one item, then both arrays have common element(s).

intersection = (a, b) ->
  [a, b] = [b, a] if a.length > b.length
  value for value in a when value in b

x = ["hello", "two", "three"]
y = ["hello"]

intersection x, y  // ["hello"]

Try it here.

Sign up to request clarification or add additional context in comments.

5 Comments

Hmmm, I am quite confused by the first line, and one of the core benefits of CoffeeScript was supposed to be increased clarity and readability of JS, right? Perhaps this is a case of some code that is clever-but-complicated, and should only be used if it actually more efficient that the clearer code which @alessioalex supplied.
@GregL - do you mean this line - intersection = (a, b) ->?
Sorry, no, I meant line #2, which is the first line of the intersection function. I have learnt just enough CS syntax so far to be able to read the first line. :-) It's more that I don't understand how that line "works".
line 2 just swaps a and b, if necessary, to make sure that b is longer than a. This reduces the number of iterations in the for-loop that follows. I find that quite legible actually (the for-loop with its implicit comprehension and return is more confusing).
@GregL - It's just a parallel assignment that few languages support to swap the values of two variables without requiring a third temporary variable. I agree though that the syntax is a bit non-intuitive, maybe because of the brackets that resemble an Array. In Ruby, we could do the same using - a, b = b, a if (a.length > b.length). The construct of putting the if conditional at the end of a statement in Ruby is something I've started to love. In JavaScript, these are known as destructuring assignments.
7

Thought I would throw my own coffeescript one-liner madness :-P

true in (val in array1 for val in array2)

Comments

2

You could try:

(true for value in array1 when value in array2).length > 0

Comments

2
contains = (item for item in array2 when item in array1)

(reverse the arrays to show double entries in array1)

Comments

1

I've made a function is_in, look at my example:

array1 = ["hello","two","three"]
array2 = ["hello"]

is_in = (array1, array2) ->
  for i in array2
    for j in array1
      if i is j then return true

console.log is_in(array1, array2)

Test here

After having a look at the intersection example, I can achieve this in another way:

intersection = (a, b) ->
  [a, b] = [b, a] if a.length > b.length
  return true for value in a when value in b

array1 = ["hello","two","three"]
array2 = ["hello"]

console.log intersection(array1, array2)

Test here

2 Comments

The former is much easier to understand - I would be curious if someone wanted to create a jsPerf.com test for this to see if there is any benefit to the intersection code you posted second.
You can create a jsPerf test yourself. Click on the two 'Test here' links and then grab the compiled JavaScript code from there. After that create a jsPerf with the 2 cases.
1

Just in case someone comes here and is looking for the difference as opposed to the intersection

difference = (val for val in array1 when val not in array2)

This will give you an array (difference) of all values in array1 but not in array2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.