Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!