过滤5级

时间:2018-03-18 20:10:00

标签: javascript

我正在尝试使用.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表达式过滤嵌套对象的数组,但我也不能。

非常感谢你。

2 个答案:

答案 0 :(得分:1)

您可以检查每个属性是一个数组并仅采用过滤值。

这种方法改变了原始数组。



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; }




答案 1 :(得分:0)

我假设你的每个对象都有这种结构:

  {
   id: "sth",
   name: "whatever"
   children: [ /***/ ]
  }

因此,递归过滤非常容易:

 function filter(arr, search){
   const result = [];
   for(const {name, id, children} of arr){
     children = filter(children, search);
     if(children.length || id === search)
       result.push({id, name, children });
   }
   return result;
 }

可用作:

  var response = filter(objList, "tiger shark");