-2

I am a beginning in javascript. So i have this data with the basic structure like so...

{
"status": "active",
"experimental": false,
"expansion": {
    "extension": [
        {
            "valueBoolean": true
        }
    ],
    "contains": [
        {
            "system": "http://snomed.info/sct",
            "code": "8989938493",
            "display": "Acute"
        },
        {
            "system": "http://snomed.info/sct",
            "code": "889080980089",
            "display": "Chronic"
        },
        {
            "system": "http://snomed.info/sct",
            "code": "889080980089",
            "display": "Fragile"
        }
    ]
}

}

I want to iterate over "contains" and put the values of "display" in an array so i can use it in a react component. I am currently struggling to achieve that. Any help will be appreciated. Thanks in advance.

2 Answers2

2

You can use the map function to transform an array:

const myObject = {
"status": "active",
"experimental": false,
"expansion": {
    "extension": [
        {
            "valueBoolean": true
        }
    ],
    "contains": [
        {
            "system": "http://snomed.info/sct",
            "code": "8989938493",
            "display": "Acute"
        },
        {
            "system": "http://snomed.info/sct",
            "code": "889080980089",
            "display": "Chronic"
        },
        {
            "system": "http://snomed.info/sct",
            "code": "889080980089",
            "display": "Fragile"
        }
    ]
}
}

const arr = myObject.expansion.contains.map(({display})=>display)
console.log(arr)
Taxel
  • 3,859
  • 1
  • 18
  • 40
  • Thank you for the response. Your code works fine here but throws " Server Error TypeError: Cannot read property 'expansion' of undefined" in my code. Console.log(data) of the fetched data works fine. don't know what the issue is. – user13171920 Aug 19 '21 at 15:50
  • Unhandled Runtime Error ReferenceError: res is not defined – user13171920 Aug 19 '21 at 15:53
0

let res = [];
const obj = {
"status": "active",
"experimental": false,
"expansion": {
    "extension": [
        {
            "valueBoolean": true
        }
    ],
    "contains": [
        {
            "system": "http://snomed.info/sct",
            "code": "8989938493",
            "display": "Acute"
        },
        {
            "system": "http://snomed.info/sct",
            "code": "889080980089",
            "display": "Chronic"
        },
        {
            "system": "http://snomed.info/sct",
            "code": "889080980089",
            "display": "Fragile"
        }
    ]
}
}

obj.expansion.contains.forEach(el => res.push(el.display));

console.log(res)
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
  • Why the re-invention of `Array.prototype.map()`? – Andreas Aug 19 '21 at 15:08
  • Thank you for the response. Your code works fine here but throws " Server Error TypeError: Cannot read property 'expansion' of undefined" in my code. Console.log(data) of the fetched data works fine. don't know what the issue is. – user13171920 Aug 19 '21 at 15:50