refactor: Use C++11 range based for loops to simplify rpc code
Summary:
This is a backport of core#19528 [2/4]
https://github.com/bitcoin/bitcoin/pull/19528/commits/fa89ca9b5bd334813fd7e7edb202c56b35076e8d
Depends on D10080
Test Plan: ninja all check-all
Reviewers: #bitcoin_abc, Fabien
Reviewed By: #bitcoin_abc, Fabien
Differential Revision: https://reviews.bitcoinabc.org/D10081