我正在尝试使用.filter过滤此数组。



var objList = [
  {
    "name": "Object0Name",
    "id": "Object0ID",
    "Object1List": [
      {
        "id": "Object1id_A1",
        "name": "Object1Name_A1",
        "Object2List": [
          {
            "id": 187,
            "name": "Object2Name_A1",
            "Object3List": [
              {
                "id": "mammal",
                "name": "mammal",
                "Object4List": [
                  {
                    "id_client": "rabbit",
                    "Currency": "EUR"
                  },
                  {
                    "id_client": "cat",
                    "Currency": "EUR",
                  },
                  {
                    "id_client": "tiger",
                    "Currency": "EUR",
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "id": "Object1id_B1",
        "name": "Object1Name_B1",
        "Object2List": [
          {
            "id": 189,
            "name": "Object2Name_B1",
            "Object3List": [
              {
                "id": "fish",
                "name": "fish",
                "Object4List": [
                  {
                    "id_client": "tiger shark",
                    "Currency": "EUR",

                  },
                  {
                    "id_client": "tuna",
                    "currency": "GBP",
                  },

                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

var response= objList.filter(function(Object0List){
			return Object0List.Object1List.filter(function(Object1List){
				return	 Object1List.Object2List.filter(function(Object2List){
					return	 Object2List.Object3List.filter(function(Object3List){
						return	 Object3List.Object4List.filter(function(Object4List){
							return Object4List.id_client==="tiger shark";

						});
					});
				});
			});
		});



var myJSON = JSON.stringify(response);
console.log('The animal is:');
console.log(myJSON);





但是过滤器不起作用。我收到所有物体。我必须收到:
[
  {
    “ name”:“ Object0Name”,
    “ id”:“ Object0ID”,
    “ Object1List”:[

  {
    "id": "Object1id_B1",
    "name": "Object1Name_B1",
    "Object2List": [
      {
        "id": 189,
        "name": "Object2Name_B1",
        "Object3List": [
          {
            "id": "fish",
            "name": "fish",
            "Object4List": [
              {
                "id_client": "tiger shark",
                "Currency": "EUR",

              }

            ]
          }
        ]
      }
    ]
  }
]


}
]

有人可以帮我找出我做错了什么吗?我确定问题是我使用的.filter函数不好,但是花了几个小时,我无法修复它。我认为我对嵌套对象不了解此功能,我试图用lambda表达式过滤嵌套对象的数组,但我也做不到。

非常感谢你。

最佳答案

您可以检查每个属性,它是一个数组,并且仅接受过滤后的值。

这种方法会变异原始数组。



function filter(array, value) {
    var temp = array.filter(o =>
        Object.keys(o).some(k => {
            var t = filter(Array.isArray(o[k]) ? o[k] : [], value);
            if (o[k] === value) {
                return true;
            }
            if (t && Array.isArray(t) && t.length) {
                o[k] = t;
                return true;
            }
        })
    );
    if (temp.length) {
        return temp;
    }
}

var array = [{ name: "Object0Name", id: "Object0ID", Object1List: [{ id: "Object1id_A1", name: "Object1Name_A1", Object2List: [{ id: 187, name: "Object2Name_A1", Object3List: [{ id: "mammal", name: "mammal", Object4List: [{ id: "rabbit", Currency: "EUR" }, { id: "cat", Currency: "EUR" }, { id: "tiger", Currency: "EUR" }] }] }] }, { id: "Object1id_B1", name: "Object1Name_B1", Object2List: [{ id: 189, name: "Object2Name_B1", Object3List: [{ id: "fish", name: "fish", Object4List: [{ id: "tiger shark", Currency: "EUR" }, { id: "tuna", currency: "GBP" }] }] }] }] }],
    result = filter(array, 'tiger shark');

console.log(result);

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

关于javascript - 过滤5级嵌套对象的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49352371/

10-10 00:28
查看更多