| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 function forEach(dict, f) { | |
| 6 for (key in dict) { | |
| 7 if (dict.hasOwnProperty(key)) | |
| 8 f(key, dict[key]); | |
| 9 } | |
| 10 } | |
| 11 | |
| 12 // Assuming |array_of_dictionaries| is structured like this: | |
| 13 // [{id: 1, ... }, {id: 2, ...}, ...], you can use | |
| 14 // lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2. | |
| 15 function lookup(array_of_dictionaries, field, value) { | |
| 16 var filter = function (dict) {return dict[field] == value;}; | |
| 17 var matches = array_of_dictionaries.filter(filter); | |
| 18 if (matches.length == 0) { | |
| 19 return undefined; | |
| 20 } else if (matches.length == 1) { | |
| 21 return matches[0] | |
| 22 } else { | |
| 23 throw new Error("Failed lookup of field '" + field + "' with value '" + | |
| 24 value + "'"); | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 exports.forEach = forEach; | |
| 29 exports.lookup = lookup; | |
| OLD | NEW |