Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
Is array objects subset of array of objects

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda