1

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);
1
  • 2
    code !== type Commented Nov 26, 2016 at 14:48

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you IzumiSy!
One more question. Now I find the object, but how can I get the value of the "coveredText"? Thanks!
Thank you, my IDE (Aptana) throws a "unexpected token"-issue for the => . But the code works
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.