Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript find json value [duplicate]

I need to search inside a json list of countries. The json is like:

[ 
{"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"

like image 389
Tropicalista Avatar asked Oct 08 '13 16:10

Tropicalista


People also ask

How to Find the duplicate values in JSON array in JavaScript?

function find_duplicate_in_array(arra1) { const object = {}; const result = []; arra1. forEach(item => { if(! object[item]) object[item] = 0; object[item] += 1; }) for (const prop in object) { if(object[prop] >= 2) { result. push(prop); } } return result; } console.

How to remove duplicate json object from JSON array in Java?

You will need to convert the JSON to Java Objects and then perform the duplicate removal operation. Added code snippet for each of the steps. Hope this helps! You will need to convert the JSON to Java Objects and then perform the duplicate removal operation.

How do you check if there are duplicates in an array JavaScript?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.


3 Answers

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>

Here's a JSFiddle.

like image 95
showdev Avatar answered Oct 25 '22 07:10

showdev


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
  }
}
like image 84
Brad Christie Avatar answered Oct 25 '22 05:10

Brad Christie


Just use the ES6 find() function in a functional way:

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>
like image 55
Penny Liu Avatar answered Oct 25 '22 06:10

Penny Liu