Reputation: 1655
I would like to look for certain values in my Json.
Search for the type "patientSize" and find the value of the "coveredText".
Actually, the console throws out found:[]
Since I have to perform some searches, I am looking for a fast and high-performance possibility. Thank you for your tips.
Example JSON
{
"annoDs": [
{
"begin": 512,
"end": 518,
"type": "patientSize",
"coveredText": "183 cm",
"additionalParameters": {
"unit": "Zentimeter",
"value": "183"
}
}
]
}
JS
var data = JSON.parse(retrievedAnnotation);
setTimeout(function() {
function getAge(code) {
return data.annoDs.filter(
function(data){
return data.code == code;
}
);
}
var found = getAge('patientSize');
console.log("found:", found);
}, 50);
Upvotes: 1
Views: 88
Reputation: 1528
The function getAge
must be like this
function getAge(code) {
return data.annoDs.filter(function(data) {
return data.type === code;
}
}
UPDATE
You can use map
to to get an array of coverdText
function getAge(code) {
return data.annoDs
.filter((data) => data.type === code)
.map((e) => e.coveredText);
}
Upvotes: 2
Reputation: 609
The problem is that you are searching for the property "code" of each element in annoDs.
You have:
data.code == code; // undefined != 'patientSize'
You should have:
function getAge(code) {
return data.annoDs.filter(
function(data){
return data.type == code;
}
);
}
var found = getAge('patientSize');
found.forEach(el => console.log(el.coveredText));
Note that filter will return every element the matching the condition. You should use find if you know that there's only one object matching the condition, because it will return the first element matching the condition.
Upvotes: 0