Wherefore art thou Troubleshooting

Tell us what’s happening:
Hey guys, I want to know why my code isn’t working. I suspect that Array.prototype.includes() method doesn’t work well with [subArr1, subArr2, … , subArrN].includes(array) kind of situation. Any ideas?

Your code so far

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  for (let i = 0; i < collection.length; i++) {
    let result = Object.entries(source).every(el => Object.entries(collection[i]).includes(el));
    if (result) {
  // Only change code above this line
  return arr;

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:

Array.includes is not a good idea when working with subarrays like you said.

Instead of using nested subarrays to compare other arrays I would break down collection to key value pair and compare them to your ‘source’.

1 Like

Can you explain your code ?

Where is your comparison of two arrays ?

  if (result) {
1 Like

Array.includes doesn’t know how to compare complex objects like {a:value_a, b:value_b} against elements in the array. You’ll have to define how to do so, e.g., compare each key/value pair in each object of the array against the source.

I wrote an example using Array.filter to illustrate (though in this case, y will be an array with the matched object):

var fruits = [{"fruit":"Banana", taste: "bland"}, {"fruit":"Orange", "taste": "acidic"}, {"fruit":"Apple", "taste": "tart"}, {"fruit":"Mango", "taste": "sweet"}];
var source = {"fruit":"Mango", "taste": "sweet"};
var y = fruits.filter(function(entry){
if (entry.fruit == source.fruit && entry.taste == source.taste) return true; 
    else return false;

Hope this helps…

1 Like

The Object.entries() method returns an array of a given object’s own enumerable property [key, value] pairs. I tried to determine the existence of a subarray in another 2-dimensional array using Array.prototype.includes() method but the method itself doesn’t work nicely with complex objects like array. XD