How to filter nested lists of objects 5 levels

0

I'm investigating how to filter lists of nested objects. But I'm not capable .... The object that I try to filter is:

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.includes("tiger");

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



var myJSON = JSON.stringify(response);
console.log('The animal is:');
console.log(myJSON);
I'm receiving all the objects, so the filter is not working. The output that I would like to obtain is all the clients that contain the word tiger:

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": "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",
                        
                      }                      
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]

I'm sure I'm using the filter incorrectly, could someone propose filtering that works with filter or lambda expressions? (without using recursion please)

Thank you very much.

    
asked by shaky 19.03.2018 в 22:07
source

1 answer

0

To do the filtering you should use the function forEach () to iterate in the list of objects but in your case to already know the name of the lists I simply access them, however I suggest that if you can change the model of your list even more generic name it would be easier to iterate in them and It would also be better for a good maintenance in the future.

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": "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",

          }]
        }]
      }]
    }
  ]
}]

objList.forEach(function(object, indice, array) {

  object.Object1List.forEach(function(object, indice, array) {
  
    object.Object2List.forEach(function(object, indice, array) {

      object.Object3List.forEach(function(object, indice, array) {

        object.Object4List.forEach(function(object, indice, array) {
        
          if (object.id_client == "tiger")
            alert("Se encontro coincidencia")
            
        });
      });

    });

  });

});
    
answered by 20.03.2018 / 00:31
source