U
U
up72019-12-10 17:08:23
JavaScript
up7, 2019-12-10 17:08:23

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
            ]
        }
    }
]

What is the best way to implement the output of values ​​(any) by recclass without enumeration?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dollar, 2019-12-10
@dollar

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); //выводим очередное значение
}

H
hzzzzl, 2019-12-10
@hzzzzl

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

D
Dmtm, 2019-12-11
@Dmtm

Boyer-Moore algorithm, just for a sample that does not change

K
krka92, 2019-12-11
@krka92

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 question

Ask a Question

731 491 924 answers to any question