mm1975 mm1975 - 6 days ago 7
Javascript Question

Search in Json for string

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);

Answer

The function getAge must be like this

function getAge(code) {
  return data.annoDs.filter(function(data) {
    return data.type === code;
  }
}
Comments