I am new to javascript usage. I have a requirement of below JSON object message.
{
"Rep": {
"out": [
{
"first": "abc",
"second": "fd",
"state": "none",
"badge": "NON",
"res": 0
},
{
"first": "xyz",
"second": "5f7209",
"state": 15,
"badge": "OH",
"res": 0
},
{
"first": "def",
"second": "b5fd",
"badge": "Pen",
"state": 3,
"res": 0
},
{
"first": "aa",
"second": "5e",
"badge": "Com",
"state": 1,
"res": 0
}
]
}
}
The requirement is I have to traverse and take 'first' key tag value from the object and check whether the value equals to some value(here it is "first" == 'aa'). if exists I have to take the value of 'badge' from the corresponding object.
Example - From the message
1)I have to check if "first == aa" exists, then I should check in the object 'res' value if it is 0,then take the value of badge, here it is "com".
2)I have to check if "first == def" exists, then I should check in the object 'res' value if it is 0,then take the value of badge, here it is "Pen".
In the similar way I have to go through each block and check "first" key value and then take corresponding badge value.
Note : We will not be not sure on how many object blocks will come in array.
let result = array['Rep']['out'].filter((v) => v.first === 'abc'); if( result.length) console.log( result[0].badge );