2

This is my object.I need to access only "oname": "Fon" and "bid": 1 object details. There is no need to access "bid": "2" object details

{
    "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": []
    }]
}

4
  • probably make use of filter Commented Aug 30, 2016 at 12:31
  • I'm not sure I understand whats being asked here... Use dot notation? object.oname and object.bun[0].bid Commented Aug 30, 2016 at 12:32
  • Something like this, jsfiddle.net/ctwhhko7 Commented Aug 30, 2016 at 12:32
  • bun.filter(function (obj) { return (obj.bid == 1); using this only get "bid": "1" object details correctly but i want "oname": "Fon" also Commented Aug 30, 2016 at 12:35

3 Answers 3

1

You can use filter method to get desired data

 var arr = {
            "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": []
            }]
        }

var oname = arr.oname;
var data = arr.bun.filter(function (a) { return a.bid == "1" });
Sign up to request clarification or add additional context in comments.

1 Comment

:i need oname and bid="1" objcet details are in single objct how it is possible
0

You can use filter to get a subset of only the data you want:

data.bun.filter(function(d){ if( d.bid == 1 ) return true; } );

Create a new object with the data you wish to have

var newObj = {
   "oname": data.oname, 
   "bun": data.bun.filter(function(d){ if( d.bid == 1 ) return true; } ) 
};

2 Comments

@it only get bid==1 object details i need "oname": "Fon" also on the result
@SHERINAS That data is accessible via: data.oname. Check my snippet again to see how you might create a new object with both keys.
0

You can use bracket notation in javascript for this like

var key = "oname";
var value = json[key];

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.