如何搜索对象的嵌套数组?

时间:2018-06-26 12:26:08

标签: javascript

我有这个对象数组,可以搜索并找到RoomName,但是如何搜索嵌套的数组或区域?

我正在使用其中任何一个获取房间名称

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

但是我想搜索区域内的内容,我已经尝试过了

var obj1 = data2.find(o => o.Areas.AreaName === 'Area 4');
console.log(obj1);

但这不起作用。 我想发生的是,如果我找到了Area 4,那么它应该返回整个对象。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>

4 个答案:

答案 0 :(得分:0)

您可以采用嵌套方法,其中Array#find用于外部数组,Array#some用于内部数组。

如果内部Array.find返回true,则

Array.some返回该项。内部回调查找具有给定名称的区域。

如果此条件为true,则内部循环将停止并向调用函数返回true,调用函数将返回带有所需对象的对象。

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" }] }];
    obj1 = data2.find(({ Areas }) => Areas.some(({ AreaName }) => AreaName === "Area 4"));

console.log(obj1);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

这似乎是先前问题的重复。 javascript中没有内置的深层对象搜索方法,但是您可以使用递归来查找嵌套对象的值。

答案 2 :(得分:0)

您可以使用filtersome来测试数组中至少一个元素的AreaName是否等于搜索到的变量。

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 AreaNameToSearch = 'Area 4';
var obj1 = data2.find(o => o.Areas.some(x => x.AreaName === AreaNameToSearch));

console.log(obj1);

答案 3 :(得分:0)

您可以制作一个双重过滤器以遍历这些区域并仅在满足条件时返回。

var areaObj = data2.filter(o=> {
  return o.Areas.filter(a=>{ 
    return a.AreaName == 'Area 4'
  }).length > 0
})

希望这会有所帮助:>

$(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 areaObj = data2.filter(o=>o.Areas.filter(a=> a.AreaName == 'Area 4').length > 0)

console.log(areaObj)

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>