Determine if requested documents exist and if they do, whether they match requested revision number Is there a better way than using 2 for loops and 2 if statements deep?

jsfiddle:

https://jsfiddle.net/bobbyrne01/bmnk9bLz/

Code:

var documentsInDatabase = [{
  id: '1',
  revision: '1'
}, {
  id: '2',
  revision: '1'
}, {
  id: '3',
  revision: '1'
}];

// incorrect revision of document with id of 1, and non-existent document of id 4
var requestedDocuments = [{
  id: '1',
  revision: '2'
}, {
  id: '4',
  revision: '1'
}];

var result = {
  ids_not_found: [],
  revision_mismatches: []
};

// 4 nested blocks deep
for (var i = 0; i &lt; documentsInDatabase.length; i++) {
  for (var j = 0; j &lt; requestedDocuments.length; j++) {
    if (documentsInDatabase[i].id === requestedDocuments[j].id) {
      if (documentsInDatabase[i].revision !== requestedDocuments[j].revision) {
        result.revision_mismatches.push(requestedDocuments[j].id);
      }

      requestedDocuments.splice(j, 1);
      break;
    }
  }
}

for (var l = 0; l &lt; requestedDocuments.length; l++) {
  result.ids_not_found.push(requestedDocuments[l].id);
}

document.getElementById('container').innerHTML = JSON.stringify(result, null, 2);
When you have code that starts to look like this, it's a sign that you need to write more functions to abstract some things away. You might put the code inside the two for loops into its own function, for example.