Hey I would like to ask if it is possible to find lets say in 10 JSON files which are looking like this:
1.json:
JavaScript
x
14
14
1
{
2
"name": "Miami",
3
"attributes": [
4
{
5
"att_type": "People",
6
"value": "1000"
7
},
8
{
9
"att_type": "Cars",
10
"value": 300
11
}
12
]
13
}
14
2.json:
JavaScript
1
14
14
1
{
2
"name": "New York",
3
"attributes": [
4
{
5
"att_type": "People",
6
"value": "5000"
7
},
8
{
9
"att_type": "Cars",
10
"value": 900
11
}
12
]
13
}
14
And so on… just different attribute values. Lets say I want to find only towns with People > 2500 and I’m not even sure if it is possible or do I need to upload the json files to some database perhaps?
Thank you.
Advertisement
Answer
JavaScript
1
33
33
1
const data = [{
2
"name": "Miami",
3
"attributes": [{
4
"att_type": "People",
5
"value": "1000"
6
},
7
{
8
"att_type": "Cars",
9
"value": 300
10
}
11
]
12
},
13
{
14
"name": "New York",
15
"attributes": [{
16
"att_type": "People",
17
"value": "5000"
18
},
19
{
20
"att_type": "Cars",
21
"value": 900
22
}
23
]
24
}
25
]
26
27
const result = data
28
// find cities with attribute `People` with value greater than 2500
29
.filter(d => +d.attributes.find(attr => attr.att_type === 'People').value > 2500)
30
// get name of the city
31
.map(d => d.name)
32
33
console.log(result)