-2

I have an object called val that looks like this:

{
    "outer": [
        {
            "key": "A",
            "value": {
                "70": 14,
                "84": 22
            }
        },
        {
            "key": "B",
            "value": {
                "70": 9,
                "84": 11
            }
        },
        {
            "key": "C",
            "value": {
                "70": 83,
                "84": 135
            }
        }
    ]
}

I can call this:


console.log(val.outer.find(x => x.key === 'A').value)
{"70": 14, "84": 22}

Is there a way I can print the value relating to "84"?

So for example, I'd like to log where Key = A and Value Key = 84.

nimgwfc
  • 1,294
  • 1
  • 12
  • 30

2 Answers2

1

Simply access the value relating to "84" by brackets:

val = {
    "outer": [
        {
            "key": "A",
            "value": {
                "70": 14,
                "84": 22
            }
        },
        {
            "key": "B",
            "value": {
                "70": 9,
                "84": 11
            }
        },
        {
            "key": "C",
            "value": {
                "70": 83,
                "84": 135
            }
        }
    ]
}
console.log(val.outer.find(x => x.key === 'A').value['84']);//print 22
XMehdi01
  • 5,538
  • 2
  • 10
  • 34
1

You can treat the object as an array and call obj['84'] or obj[84]:

var val = {
    "outer": [
        {
            "key": "A",
            "value": {
                "70": 14,
                "84": 22
            }
        },
        {
            "key": "B",
            "value": {
                "70": 9,
                "84": 11
            }
        },
        {
            "key": "C",
            "value": {
                "70": 83,
                "84": 135
            }
        }
    ]
};

console.log(val.outer.find(x => x.key === 'A').value['84']);
chrwahl
  • 8,675
  • 2
  • 20
  • 30