| 1358 | // Finds a subobject that has the given key, if there is one. |
| 1359 | // Returns undefined otherwise. |
| 1360 | function findObjectWithKey(root, key) { |
| 1361 | if (typeof root !== 'object') { |
| 1362 | return; |
| 1363 | } |
| 1364 | if (Array.isArray(root)) { |
| 1365 | for (var item of root) { |
| 1366 | const answer = findObjectWithKey(item, key); |
| 1367 | if (answer) { |
| 1368 | return answer; |
| 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | if (root && root[key]) { |
| 1373 | return root; |
| 1374 | } |
| 1375 | for (var subkey in root) { |
| 1376 | const answer = findObjectWithKey(root[subkey], key); |
| 1377 | if (answer) { |
| 1378 | return answer; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | module.exports = RestQuery; |
| 1384 | // For tests |