Answer the question
In order to leave comments, you need to log in
What is the best way to implement search for values in json?
There is a json like
[
{
"name": "Adhi Kot",
"id": "379",
"nametype": "Valid",
"recclass": "EH4",
"mass": "4239",
"fall": "Fell",
"year": "1919-01-01T00:00:00.000",
"reclat": "32.100000",
"reclong": "71.800000",
"geolocation": {
"type": "Point",
"coordinates": [
71.8,
32.1
]
}
}
]
Answer the question
In order to leave comments, you need to log in
Through indexOf it is possible. This is the most optimal thing that comes to mind, i.e. the most direct search "on the forehead" without unnecessary calculations. Only a search without a search is more optimal, when the indices are known in advance, from where you need to take the values.
var json_str = `,{"name":"Adhi Kot","id":"379","nametype":"Valid","recclass":"EH4","mass":"4239","fall":"Fell","year":"1919-01-01T00:00:00.000","reclat":"32.100000","reclong":"71.800000","geolocation":{"type":"Point","coordinates":[71.8,32.1]}},{"name":"Adzhi-Bogdo (stone)","id":"390","nametype":"Valid","recclass":"LL3-6","mass":"910","fall":"Fell","year":"1949-01-01T00:00:00.000","reclat":"44.833330","reclong":"95.166670","geolocation":{"type":"Point","coordinates":[95.16667,44.83333]}}`;
var i = 0;
while((i=json_str.indexOf('"recclass":"',i))!==-1) {
i+=12;
let j = json_str.indexOf('"',i);
let val = json_str.substring(i,j);
console.log(val); //выводим очередное значение
}
is it an array of objects?
data = [
{"name":"Adhi Kot","id":"379","nametype":"Valid","recclass":"EH4","mass":"4239","fall":"Fell","year":"1919-01-01T00:00:00.000","reclat":"32.100000","reclong":"71.800000","geolocation":{"type":"Point","coordinates":[71.8,32.1]}},
{"name":"Adzhi-Bogdo (stone)","id":"390","nametype":"Valid","recclass":"LL3-6","mass":"910","fall":"Fell","year":"1949-01-01T00:00:00.000","reclat":"44.833330","reclong":"95.166670","geolocation":{"type":"Point","coordinates":[95.16667,44.83333]}}
]
const find = (rec, key) => (data.find(v => v.recclass === rec) || {})[key]
find('EH4', 'mass')
// 4239
If without deserialization, then you can parse the key and value.
1. When we meet "recclass" - check the value for a match. If it matches, find it.
2. You can have an array that will contain only the values of "recclass" in accordance with the elements in the json.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question