Skip to main content

Overview

The airline-codes package exports a Backbone Collection, giving you access to all standard Backbone Collection methods. This page documents the most commonly used methods with airline-specific examples.

Query Methods

findWhere

Find the first model that matches the specified attributes.
attributes
object
required
Object containing attribute key-value pairs to match
return
Model | undefined
Returns the first matching Backbone Model, or undefined if not found
const airlines = require('airline-codes');

// Find by IATA code
const delta = airlines.findWhere({ iata: 'DL' });
console.log(delta.get('name')); // "Delta Air Lines"

// Find by ICAO code
const lufthansa = airlines.findWhere({ icao: 'DLH' });
console.log(lufthansa.get('country')); // "Germany"

// Find by multiple attributes
const airline = airlines.findWhere({ 
  country: 'United States', 
  iata: 'AA' 
});

where

Get all models that match the specified attributes.
attributes
object
required
Object containing attribute key-value pairs to match
return
Array<Model>
Returns an array of matching Backbone Models
// Get all airlines from a specific country
const japaneseAirlines = airlines.where({ country: 'Japan' });
console.log(`Japanese airlines: ${japaneseAirlines.length}`);

// Get all active airlines
const activeAirlines = airlines.where({ active: 'Y' });

// Results are Backbone Models
japaneseAirlines.forEach(model => {
  console.log(model.get('name'));
});

filter

Filter the collection using a custom predicate function.
predicate
function
required
Function that returns true for models to include
return
Array<Model>
Returns an array of Backbone Models that pass the test
// Find airlines with IATA codes starting with 'A'
const aAirlines = airlines.filter(model => {
  const iata = model.get('iata');
  return iata && iata.startsWith('A');
});

// Find airlines with callsigns
const withCallsigns = airlines.filter(model => {
  const callsign = model.get('callsign');
  return callsign && callsign.length > 0;
});

// Complex filtering
const usCarriers = airlines.filter(model => {
  return model.get('country') === 'United States' &&
         model.get('active') === 'Y';
});

find

Find the first model that passes a predicate function.
predicate
function
required
Function that returns true for the model to find
return
Model | undefined
Returns the first matching Backbone Model, or undefined
// Find first airline with specific ICAO code
const airline = airlines.find(model => {
  return model.get('icao') === 'UAL';
});

// Find first airline with long name
const longName = airlines.find(model => {
  return model.get('name').length > 30;
});

Access Methods

at

Get a model at a specific index position.
index
number
required
Zero-based index position
return
Model | undefined
Returns the Backbone Model at the specified index
// Get the first airline (alphabetically by name)
const first = airlines.at(0);
console.log(first.get('name'));

// Get the 10th airline
const tenth = airlines.at(9);

// Get the last airline
const last = airlines.at(airlines.length - 1);

get

Get a model by its id attribute.
id
string | number
required
The id of the model to retrieve
return
Model | undefined
Returns the Backbone Model with the specified id
// Get airline by id
const airline = airlines.get('24');
console.log(airline.get('name')); // "American Airlines"

// Check if exists
if (airlines.get('999')) {
  console.log('Found');
} else {
  console.log('Not found');
}

first

Get the first model in the collection.
return
Model | undefined
Returns the first Backbone Model in the collection
// Get first airline (alphabetically by name due to comparator)
const first = airlines.first();
console.log(first.get('name'));

last

Get the last model in the collection.
return
Model | undefined
Returns the last Backbone Model in the collection
// Get last airline (alphabetically by name)
const last = airlines.last();
console.log(last.get('name'));

Data Extraction Methods

pluck

Extract a specific attribute from all models.
attribute
string
required
Name of the attribute to extract
return
Array<any>
Returns an array of attribute values
// Get all airline names
const names = airlines.pluck('name');
console.log(names); // ['1Time Airline', '40-Mile Air', ...]

// Get all IATA codes
const iataCodes = airlines.pluck('iata');

// Get all countries
const countries = airlines.pluck('country');
const uniqueCountries = [...new Set(countries)];
console.log(`Airlines from ${uniqueCountries.length} countries`);

toJSON

Convert the entire collection to a plain JavaScript array.
return
Array<object>
Returns an array of plain JavaScript objects
// Export as plain JSON
const airlinesData = airlines.toJSON();
console.log(airlinesData[0]);
// {
//   id: '1',
//   name: 'Private flight',
//   alias: '',
//   iata: '-',
//   icao: 'N/A',
//   callsign: '',
//   country: '',
//   active: 'Y'
// }

// Use with JSON.stringify
const json = JSON.stringify(airlines.toJSON(), null, 2);

// Filter then convert to JSON
const usAirlines = airlines.where({ country: 'United States' });
const usData = usAirlines.map(model => model.toJSON());

Iteration Methods

forEach

Iterate over each model in the collection.
callback
function
required
Function called for each model with arguments: (model, index)
// Log all airline names
airlines.forEach((model, index) => {
  console.log(`${index + 1}. ${model.get('name')}`);
});

// Collect specific data
const summary = [];
airlines.forEach(model => {
  summary.push({
    name: model.get('name'),
    code: model.get('iata')
  });
});

map

Transform each model using a function.
callback
function
required
Function called for each model that returns the transformed value
return
Array<any>
Returns an array of transformed values
// Create simplified airline objects
const simplified = airlines.map(model => {
  return {
    name: model.get('name'),
    iata: model.get('iata'),
    country: model.get('country')
  };
});

// Create display labels
const labels = airlines.map(model => {
  const name = model.get('name');
  const iata = model.get('iata');
  return `${name} (${iata})`;
});

reduce

Reduce the collection to a single value.
callback
function
required
Function called for each model with arguments: (accumulator, model)
initialValue
any
Initial value for the accumulator
return
any
Returns the final accumulated value
// Count airlines by country
const byCountry = airlines.reduce((acc, model) => {
  const country = model.get('country');
  acc[country] = (acc[country] || 0) + 1;
  return acc;
}, {});

console.log(byCountry['United States']); // Number of US airlines

// Build a lookup map by IATA code
const iataMap = airlines.reduce((map, model) => {
  const iata = model.get('iata');
  if (iata && iata !== '-') {
    map[iata] = model.toJSON();
  }
  return map;
}, {});

console.log(iataMap['AA']); // American Airlines data

some

Check if any model passes a predicate test.
predicate
function
required
Function that returns true or false for each model
return
boolean
Returns true if any model passes the test
// Check if any airline is from Iceland
const hasIcelandic = airlines.some(model => {
  return model.get('country') === 'Iceland';
});

// Check if any airline has a specific IATA code
const hasCode = airlines.some(model => {
  return model.get('iata') === 'XX';
});

every

Check if all models pass a predicate test.
predicate
function
required
Function that returns true or false for each model
return
boolean
Returns true if all models pass the test
// Check if all airlines have an id
const allHaveIds = airlines.every(model => {
  return model.get('id') !== undefined;
});

// Check if all airlines are active
const allActive = airlines.every(model => {
  return model.get('active') === 'Y';
});

Sorting Methods

sortBy

Sort models by a specific attribute or function.
attribute
string | function
required
Attribute name or iterator function to sort by
return
Array<Model>
Returns a sorted array of Backbone Models
This returns a new sorted array without modifying the original collection. The collection itself is already sorted by name due to the comparator.
// Sort by IATA code
const byIata = airlines.sortBy('iata');

// Sort by custom function
const byCallsignLength = airlines.sortBy(model => {
  return model.get('callsign').length;
});

// Results are still Backbone Models
byIata.forEach(model => {
  console.log(model.get('iata'));
});

Utility Methods

size / length

Get the number of models in the collection.
return
number
Returns the total count of models
// Get total count
const total = airlines.length;
console.log(`Total airlines: ${total}`);

// Also available as method
const count = airlines.size();

isEmpty

Check if the collection has no models.
return
boolean
Returns true if the collection is empty
if (airlines.isEmpty()) {
  console.log('No airlines found');
} else {
  console.log(`Found ${airlines.length} airlines`);
}

indexOf

Get the index of a specific model.
model
Model
required
The Backbone Model to find
return
number
Returns the index, or -1 if not found
const airline = airlines.findWhere({ iata: 'AA' });
const index = airlines.indexOf(airline);
console.log(`American Airlines is at index ${index}`);

Method Chaining

Many Backbone methods support chaining through the chain() method:
const usAirlineNames = airlines.chain()
  .filter(model => model.get('country') === 'United States')
  .filter(model => model.get('active') === 'Y')
  .sortBy('name')
  .map(model => model.get('name'))
  .value();

console.log(usAirlineNames);

Working with Models

Once you retrieve a model from the collection, you can access its attributes:
const airline = airlines.findWhere({ iata: 'BA' });

// Get individual attributes
const name = airline.get('name');
const country = airline.get('country');
const icao = airline.get('icao');

// Get all attributes as plain object
const data = airline.toJSON();
console.log(data);

// Check if attribute exists
if (airline.has('callsign')) {
  console.log(airline.get('callsign'));
}

Next Steps

Data Fields

Learn about all available fields in airline objects

Build docs developers (and LLMs) love