SHERIN AS
SHERIN AS

Reputation: 93

how to get matching objects in an array of objects?

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?

Upvotes: 1

Views: 57

Answers (1)

James
James

Reputation: 1446

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
});

Upvotes: 3

Related Questions