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

168
Visualizações
use every and filter function together in JavaScript

I have two arrays of strings. where I want to see if one array has any value common from another array and then print the array which has unique values only. For this I am performing a JavaScript every function on both the arrays which returns a Boolean if one array has values common from another array and then if the condition is true, I am doing a filter on an array to show the data. Here are the arrays:

marketingCarrierCode: ["DL", "AA"]
operatingCarrierCode: ["AA", "AA"]

now I am doing every and filter like this way:

const hasSameCarrierCode = operatingAirlineCode.every(
    code => marketingAirlineCode.indexOf(code) !== -1,
  );
  const listOfOperatingCarriers = operatingAirlineCode.filter(
    code => marketingAirlineCode.indexOf(code) === -1,
  );

this gives me:

hasSameCarrierCode: true
listOfOperatingCarriers: []

now I want to perform every and filter function in one function, how can I do that?

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

0

Doesn't need to first function. You can use thie:

const listOfOperatingCarriers = operatingAirlineCode.filter(
  code => marketingAirlineCode.indexOf(code) === -1,
);

listOfOperatingCarriers.length !== operatingAirlineCode.length   => true,
listOfOperatingCarriers => []
about 4 years ago · Juan Pablo Isaza Relatório

0

In your sample code, "for each" code in operatingCarrierCode, you are checking if it exists in marketingAirlineCode. If any code doesn't exist in marketingAirlineCode, then test is failed i.e. hasSameCarrierCode should be false. Also, you add the uncommon code in list listOfOperatingCarriers. All this can be combined into one forEach as below:

const marketingAirlineCode = ["DL", "AA"]
const operatingCarrierCode = ["AA", "AA"]

let hasSameCarrierCode = true;
let listOfOperatingCarriers = [];

operatingCarrierCode.forEach(code => {
  if (marketingAirlineCode.indexOf(code) === -1) {
    hasSameCarrierCode = false;
    listOfOperatingCarriers.push(code);
  }
})

console.log(`hasSameCarrierCode:`, hasSameCarrierCode)
console.log(`listOfOperatingCarriers:`, listOfOperatingCarriers);

A much smaller version could be:

const marketingAirlineCode = ["DL", "AA"]
const operatingCarrierCode = ["AA", "AA"]

const listOfOperatingCarriers = operatingCarrierCode.filter(code => marketingAirlineCode.indexOf(code) === -1);
const hasSameCarrierCode = listOfOperatingCarriers.length === 0;

console.log(`hasSameCarrierCode:`, hasSameCarrierCode)
console.log(`listOfOperatingCarriers:`, listOfOperatingCarriers);

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