-1

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.

4
  • Not sure what the problem is.... sounds like you need a loop Commented Mar 18, 2021 at 2:29
  • use array filter for this. For example let result = array['Rep']['out'].filter((v) => v.first === 'abc'); if( result.length) console.log( result[0].badge ); Commented Mar 18, 2021 at 2:38
  • or use array find() function to find first occurance Commented Mar 18, 2021 at 2:43
  • You are missing double quote at this position "second": "5e, Commented Mar 18, 2021 at 3:59

3 Answers 3

0
object={
  "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
      }
    ]
  }
}

in_list=object.Rep.out
in_list.forEach((arr)=>{
  if (arr.first=="def"){
    console.log(arr.badge);
  }
});

You can read values in JSON by JSON.Key and You can loop over Array using forEach() . Then you can use conditions to get related badge value. Hope this will be helpful !!

Sign up to request clarification or add additional context in comments.

Comments

0

suppose you have your json message stored in a variable called jsonMessage like this:

const jsonMessage = {
  "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
      }
    ]
  }
}

// Now you can refers "out" array directly, its called destructuring (google it, its pretty usefull)
const { Rep: { out } } = jsonMessage

// Filter objects that fulfill conditions
const validObjects = out.filter( obj => {
    return (obj.first === "aa" && obj.res === 0) || (obj.first === "def" && obj.res === 0)
})

// Now you have an array with valid objects, for each object in array you can recover badge value
validObjects.forEach( obj => {
    console.log(obj.badge)
})

Comments

0

You can use map() method to play with condition for which you want.

const jsonMessage = {
    "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
        }]
    }
}

jsonMessage.Rep.out.map(function(params) {
    if((params.first=="aa" && params.res ==0) || (params.first === "def" && params.res == 0)) {
        console.log(params.badge);
    } 
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.