1

I know this results in an array with all the common elements from arrays, but I don't understand how that line of code achieves that. When I look up shift() it looks like it should take out the first element of arrays, which is an array itself. Basically, I'm confused, wondering if someone could walk me through it with dummy steps what actually happens. Thanks!

var arrays = [
['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'],
['taco', 'fish', 'apple', 'pizza'],
['banana', 'pizza', 'fish', 'apple']
];


var result = arrays.shift().filter(
function(v) {return arrays.every(
function(a) {return a.indexOf(v) !== -1;});
});

5 Answers 5

1

The shift() function will remove the first array from arrays and return it.

result === arrays.shift() === ["apple", "orange", "banana", "pear", "fish", "pancake", "taco", "pizza"]

arrays === [['taco', 'fish', 'apple', 'pizza'],['banana', 'pizza', 'fish', 'apple']]

You then filter() out from results every item that exists in both remaining arrays. You then end up with ["apple", "fish", "pizza"] as your final answer.

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

Comments

1
var arrays = [
['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'],
['taco', 'fish', 'apple', 'pizza'],
['banana', 'pizza', 'fish', 'apple']
];

arrays.shift() as you correctly saw takes the first element ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'] You then filter over every element of that array (making each current element you're filtering on available as v). You then check that each subarray constains that element v. If that criteria is met (indexOf does not return -1 as specified via API), that element is returned.

Ultimately your result array consists of [ 'apple', 'fish', 'pizza' ].

Comments

1

It may help to break some of this code up, we can split out the filter function to make the main code a bit clearer:

var result = arrays.shift().filter(myFilterFunc);

function myFilterFunc(element) {
  return arrays.every(function(a){
    return a.indexOf(element) !== -1;
  });
}

Array.prototype.shift() will mutate the array by removing and returning the first element:

var arrays = [
  ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'],
  ['taco', 'fish', 'apple', 'pizza'],
  ['banana', 'pizza', 'fish', 'apple']
];

arrays.shift() //returns ['apple', 'orange', 'banana', etc...]
//arrays now equals [ ['taco', 'fish', etc.], ['banana', 'pizza', etc.]

After the shift occurs, you run Array.prototype.filter(), which accepts a filtering function as its argument. This will return a new array with all elements which returned true in the filter function. In this case, the condition in the filter function is running Array.prototype.every() on the arrays array. every() returns true if every element in the array passes a particular condition:

a.indexOf(element) //checks if element is in array a

Comments

1

See added comments:

var result = arrays.shift().filter(       // Grab first array, and filter
  function(v) {                           // each value to check if:
     return arrays.every(                 // all of the remaining arrays
         function(a) {                    
           return a.indexOf(v) !== -1;    // contain a matching value
         }
     );
  }
);

Comments

0

For the following code:

var arrays = [
['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'],
['taco', 'fish', 'apple', 'pizza'],
['banana', 'pizza', 'fish', 'apple']
];

var result = arrays.shift().filter(
function(v) {return arrays.every(
function(a) {return a.indexOf(v) !== -1;});
});

Here what's happening statement by statement:

var result = arrays.shift().filter(
   ...
});

For arrays.shift() the first array element ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'] is returned, this is used as the base for the result. The .filter() method will modify this array.

function(v) {return arrays.every(
   ...
});

Which means for every array in the 2D array arrays, in this case v represents each of the elements in the base array ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza']. While a means each of the arrays inside arrays.

function(a) {return a.indexOf(v) !== -1;});

Finally this means that for all they arrays (a's) if it exist inside that base array v's then it returns true, otherwise if the element inside ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'] isn't in the other arrays then it returns false and that element is removed.

In other words all for each element in ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'], there must be a matching element for the other arrays inside arrays.

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.