1

I'm trying to write a function to check if the array of objects is a subset of another array of objects and I want it to return a boolean.

Here's what I have so far:

var arr1 = [
    { answer: "dispersed", question: "where_is_your_flare_located" },
    { answer: "continuous", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
];

var arr2 = [
    { question: 'where_is_your_flare_located', answer: 'dispersed' },
    { question: 'what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions', answer: 'periodic' },
    { question: 'how_long_is_the_flare_expected_to_be_in_operation', answer: 'n_a' },
    { question: 'am_i_managing_an_existing_flare_or_planning_to_install_a_new_one', answer: 'n_a' },
    { question: 'do_i_have_access_to_assistance_gases', answer: '' },
    { question: 'do_i_have_access_to_purge_gas', answer: '' },
];

var compare = arr2.filter(function(o) {
    var resultQuestion = o.question;
    var resultAnswer = o.question;

    return arr1.some(function(i) {
        var filterQuestion = i.question;
        var filterAnswer = i.question;
        return ((filterQuestion === resultQuestion) && (resultAnswer === filterAnswer));
    });
});

console.log(compare)

I don't understand what I'm doing wrong. I will appreciate any help or explanation.

danh
  • 62,181
  • 10
  • 95
  • 136
Rafal Adam Krohne
  • 437
  • 1
  • 3
  • 15
  • So you want to return true if all elements in `arr1` appear in `arr2`? – Jamiec Dec 07 '21 at 16:25
  • _"...what I'm doing wrong."_ - Why do you think there's something wrong with your script? Explain the actual behavior and add any error messages you might see in the console ([How do I ask a good question?](https://stackoverflow.com/help/how-to-ask)) – Andreas Dec 07 '21 at 16:27
  • 1
    Either a dup, or a useful read: https://stackoverflow.com/a/48211214/294949. – danh Dec 07 '21 at 16:28
  • _"The [`filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) method creates **a new array with all elements that pass the test** implemented by the provided function."_ – Andreas Dec 07 '21 at 16:28
  • 1
    You assign `var resultAnswer = o.question;` but this should likely be `var resultAnswer = o.answer;`. You have the same issue with `filterAnswer`. – h0r53 Dec 07 '21 at 16:31
  • "...what I'm doing wrong." - Why do you think there's something wrong with your script? Explain the actual behavior and add any error messages you might see in the console" I couldn't figure out how to write the function to get required result :/ But the solution is in the answer below :) Thank you for help! – Rafal Adam Krohne Dec 09 '21 at 11:35

1 Answers1

3

This is simply a case of seeing if every element in arr1 exists in arr2

var arr1 = [
    { answer: "dispersed", question: "where_is_your_flare_located" },
    { answer: "continuous", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
    
    // comment the above and uncomment below to return true 
    //{ answer: "periodic", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
];

var arr2 = [
    { question: 'where_is_your_flare_located', answer: 'dispersed' },
    { question: 'what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions', answer: 'periodic' },
    { question: 'how_long_is_the_flare_expected_to_be_in_operation', answer: 'n_a' },
    { question: 'am_i_managing_an_existing_flare_or_planning_to_install_a_new_one', answer: 'n_a' },
    { question: 'do_i_have_access_to_assistance_gases', answer: '' },
    { question: 'do_i_have_access_to_purge_gas', answer: '' },
];

const isSubset = arr1.every(a1 => arr2.find(a2 => a1.answer == a2.answer && a1.question == a2.question));

console.log(isSubset);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 2
    It would be more pure to use `.some()`, it returns a boolean value, whereas `.find()` returns the first matching object (which is then treated as a bool). The final result will be the same. – Peter B Dec 07 '21 at 16:37
  • This has worked perfectly for me! Thank you so much!!!! – Rafal Adam Krohne Dec 09 '21 at 11:30