Simplifying conditional spaghetti

To illustrate too much cyclomatic complexity and how we should approach simplifying it, we're going to be refactoring a piece of code that is responsible for deriving a set of ID numbers and types from a set of licenses:

function getIDsFromLicenses(licenses) {  const ids = [];  for (let i = 0; i < licenses.length; i++) {    let license = licenses[i];    if (license.id != null) {      if (license.id.indexOf('c') === 0) {        let nID = Number(license.id.slice(1));        if (nID >= 1000000) {          ids.push({ type: 'car', digits: nID });        } else {          ids.push({ type: 'car_old', digits: nID });        }      } else if (license.id.indexOf('h') === 0) {        ids.push({          type: 'hgv',          digits: Number(license.id.slice(1))        }); } else if (license.id.indexOf('m') === ...

Get Clean Code in JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.