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?
Rory McCrossan
338k41 gold badges322 silver badges353 bronze badges
-
1it will be easier if you considered it while building your objectAnonymous Duck– Anonymous Duck2016年08月05日 07:07:46 +00:00Commented Aug 5, 2016 at 7:07
-
post what you tryed or how you created the object.Sarath Kumar– Sarath Kumar2016年08月05日 07:11:49 +00:00Commented Aug 5, 2016 at 7:11
1 Answer 1
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
});
answered Aug 5, 2016 at 7:24
James
1,4761 gold badge13 silver badges25 bronze badges
lang-js