function getId(array, id) {for (var i = 0, len = array.length; i < len; i++) {if (array[i].id === id) {return array[i];}}return null; // Nothing found}
使用ECMAScript 5的Array方法也是如此:
function getId(array, id) {var obj = array.filter(function (val) {return val.id === id;});
// Filter returns an array, and we just want the matching item.return obj[0];}
if (result.length === 0) {// no result found} else if (result.length === 1) {// property found, access the foo property using result[0].foo} else {// multiple items found}
var retObj ={};$.each(ArrayOfObjects, function (index, obj) {
if (obj.id === '5') { // id.toString() if it is int
retObj = obj;return false;}});return retObj;
var indexer = {};for (var i = 0; i < array.length; i++) {indexer[array[i].id] = parseInt(i);}
//Then you can access object properties in your array usingarray[indexer[id]].property
var getKeyValueById = function(array, key, id) {var testArray = array.slice(), test;while(test = testArray.pop()) {if (test.id === id) {return test[key];}}// return undefined if no matching id is found in arrayreturn;}
var myArray = [{'id':'73', 'foo':'bar'}, {'id':'45', 'foo':'bar'}]var result = getKeyValueById(myArray, 'foo', '45');
// result is 'bar', obtained from object with id of '45'
const myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}];const res = myArray.find(x => x.id === '100')?.foo; // No error!console.log(res); // undefined when the object cannot be found
function A(arr, id) {return arr.find(o=> o.id==id);}
function B(arr, id) {let idx= arr.findIndex(o=> o.id==id);return arr[idx];}
function C(arr, id) {return arr.filter(o=> o.id==id)[0];}
function D(arr, id) {return arr.reduce((a, b) => (a.id==id && a) || (b.id == id && b));}
function E(arr, id) {for (var i = 0; i < arr.length; i++) if (arr[i].id==id) return arr[i];return null;}
function F(arr, id) {var retObj ={};$.each(arr, (index, obj) => {if (obj.id == id) {retObj = obj;return false;}});return retObj;}
function G(arr, id) {return $.grep(arr, e=> e.id == id )[0];}
function H(arr, id) {return $.map(myArray, function(val) {return val.id == id ? val : null;})[0];}
function I(arr, id) {return _.find(arr, o => o.id==id);}
let J = (()=>{let cache = new Map();return function J(arr,id,el=null) {return cache.get(id) || (el=arr.find(o=> o.id==id), cache.set(id,el), el);}})();
function K(arr, id) {return mapK.get(id)}
function L(arr, id) {return mapL[id];}
// -------------// TEST// -------------
console.log('Find id=5');
myArray = [...Array(10)].map((x,i)=> ({'id':`${i}`, 'foo':`bar_${i}`}));const mapK = new Map( myArray.map(el => [el.id, el]) );const mapL = {}; myArray.forEach(el => mapL[el.id]=el);
[A,B,C,D,E,F,G,H,I,J,K,L].forEach(f=> console.log(`${f.name}: ${JSON.stringify(f(myArray, '5'))}`));
console.log('Whole array',JSON.stringify(myArray));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
This snippet only presents tested codes