1

This is my object. Inside the bun array I have 2 objects. I need to access only "oid": 1 and "bid": 1 object details. There is no need to access the second object.

{
    "oid": "1",
    "oname": "Fon",
    "bun": [{
        "bid": "1",
        "bname": "Ets",
        "dep": [{
            "did": "1",
            "dname": "Dptment",
            "pids": [{
                "pid": "1",
                "st": "active"
            }, {
                "pid": "2",
                "st": "active"
            }]
        }]
    }, {
        "bid": "2",
        "bname": "US",
        "description": "unit2",
        "dep": []
    }]
}

How it is possible?

2
  • 1
    it will be easier if you considered it while building your object Commented Aug 5, 2016 at 7:07
  • post what you tryed or how you created the object. Commented Aug 5, 2016 at 7:11

1 Answer 1

3

One way to achieve is using filter.

let jsObj = {
  "oid": "1",
  "oname": "Fon",
  "bun": [{
    "bid": "1",
    "bname": "Ets",
    "dep": [{
      "did": "1",
      "dname": "Dptment",
      "pids": [{
        "pid": "1",
        "st": "active"
      }, {
        "pid": "2",
        "st": "active"
      }]
    }]
  }, {
    "bid": "2",
    "bname": "US",
    "description": "unit2",
    "dep": []
  }]
};

jsObj.bun.filter((b) => {
  return b.bid == 1
});
Sign up to request clarification or add additional context in comments.

2 Comments

what does "b" means?
@SHERINAS it's a variable name. b in this case is an object from bun array.

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.