I have this array of objects and I can search and find the RoomName, but how do I search through the nested arrays or Areas?
I am getting the RoomName by using any of these
var obj1 = data2.find(o => o.RoomName === 'Room 4');
console.log(obj1);
var abc = data2[data2.findIndex(x => x.RoomName == "Room 1")];
console.log(abc);
var foundValue = data2.filter(obj=>obj.RoomName === 'Room 2');
console.log(foundValue);
console.log(data2.filter(function (arr) { return arr.RoomName == 'Room 4' })[0]);
But I would like to search through the Area's, I have tried this
var obj1 = data2.find(o => o.Areas.AreaName === 'Area 4');
console.log(obj1);
But that isn't working. What I would like to happen is if I find Area 4, then it should return the whole object.. IE
{ "RoomID": 4, "RoomName": "Room 4", "Areas": [{ "id": 4, "AreaName": "Area 4" }, { "id": 40, "AreaName": "Area 40" }] }
$(document).ready(function(){
var data2 = [
{ "RoomID": 1, "RoomName": "Room 1", "Areas": [{ "id": 1, "AreaName": "Area 1" }, { "id": 10, "AreaName": "Area 10" }] },
{ "RoomID": 2, "RoomName": "Room 2", "Areas": [{ "id": 2, "AreaName": "Area 2" }, { "id": 20, "AreaName": "Area 20" }] },
{ "RoomID": 3, "RoomName": "Room 3", "Areas": [{ "id": 3, "AreaName": "Area 3" }, { "id": 30, "AreaName": "Area 30" }, { "id": 35, "AreaName": "Area 35" }] },
{ "RoomID": 4, "RoomName": "Room 4", "Areas": [{ "id": 4, "AreaName": "Area 4" }, { "id": 40, "AreaName": "Area 40" }] }
];
var obj1 = data2.find(o => o.RoomName === 'Room 4');
console.log(obj1);
var abc = data2[data2.findIndex(x => x.RoomName == "Room 1")];
console.log(abc);
var foundValue = data2.filter(obj=>obj.RoomName === 'Room 2');
console.log(foundValue);
console.log(data2.filter(function (arr) { return arr.RoomName == 'Room 4' })[0]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>