相关文章推荐
骑白马的蚂蚁  ·  时间库Moment.js ...·  1 年前    · 
豁达的帽子  ·  Dbeaver无法连接 - CSDN文库·  1 年前    · 
粗眉毛的人字拖  ·  Python ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams {"name": "Afghanistan", "code": "AF"}, {"name": "Åland Islands", "code": "AX"}, {"name": "Albania", "code": "AL"}, {"name": "Algeria", "code": "DZ"}

I get from database only the code and would output the entire name. So if I get "AL" I would like to retrieve from json "Albania"

I suggest using JavaScript's Array method filter() to identify an element by value. It filters data by using a "function to test each element of the array. Return true to keep the element, false otherwise.."

The following function filters the data, returning data for which the callback returns true , i.e. where data.code equals the requested country code.

function getCountryByCode(code) {
  return data.filter(
      function(data){ return data.code == code }
var found = getCountryByCode('DZ');

See the demonstration below:

var data = [{
  "name": "Afghanistan",
  "code": "AF"
  "name": "Åland Islands",
  "code": "AX"
  "name": "Albania",
  "code": "AL"
  "name": "Algeria",
  "code": "DZ"
function getCountryByCode(code) {
  return data.filter(
    function(data) {
      return data.code == code
var found = getCountryByCode('DZ');
document.getElementById('output').innerHTML = found[0].name;
<div id="output"></div>
I know this is answer is over 5 years old, but the statement that it return "only the value for which the callback returns true" is incorrect. It returns an array of objects where the value in each matched object causes the callback to return true. Your code does recognise this, but I feel the statement is misleading – freefaller Apr 4, 2019 at 14:42
var obj = [
  {"name": "Afghanistan", "code": "AF"}, 
  {"name": "Åland Islands", "code": "AX"}, 
  {"name": "Albania", "code": "AL"}, 
  {"name": "Algeria", "code": "DZ"}
// the code you're looking for
var needle = 'AL';
// iterate over each element in the array
for (var i = 0; i < obj.length; i++){
  // look for the entry with a matching `code` value
  if (obj[i].code == needle){
     // we found it
    // obj[i].name is the matched result
                @Tropicalista In fact, you should move the filter logic to your database engine! The DB will always be the fastest when it comes to filtering, ordering or grouping (assuming you set correct indices).
– ComFreek
                Oct 8, 2013 at 17:14
                @jaibalaji not if you support IE. Also keep in mind this post is now going on 5 years old, compatibility has changed.
– Brad Christie
                Jul 17, 2020 at 18:15
var data=[{name:"Afghanistan",code:"AF"},{name:"Åland Islands",code:"AX"},{name:"Albania",code:"AL"},{name:"Algeria",code:"DZ"}];
let country = data.find(el => el.code === "AL");
// => {name: "Albania", code: "AL"}
console.log(country["name"]);

or Lodash _.find:

var data=[{name:"Afghanistan",code:"AF"},{name:"Åland Islands",code:"AX"},{name:"Albania",code:"AL"},{name:"Algeria",code:"DZ"}];
let country = _.find(data, ["code", "AL"]);
// => {name: "Albania", code: "AL"}
console.log(country["name"]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
I'm not at all sure it's true that "this method guarantees the instant lookup". Is there any guarantee that the implementation does anything but a sequential lookup? If not, can we be certain that no significant browser implementation does a sequential lookup? – Auspex Jul 11, 2017 at 9:41

Making more general the @showdev answer.

var getObjectByValue = function (array, key, value) {
    return array.filter(function (object) {
        return object[key] === value;

Example:

getObjectByValue(data, "code", "DZ" );