2

I am trying filter this array with .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);

But the filter doesn't work. I am receiving all objects. I must receive: [ { "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",

              }                 

            ]
          }
        ]
      }
    ]
  }
]

} ]

Could someone help me find out what I'm doing wrong? I'm sure the problem is that I'm using the .filter function badly but it took several hours and I'm not capable of fixing it. I think that I do not understand this function for nested objects, I tried to filter the array of nested objects with lambda expressions but I'm also not able.

Thanks you very much.

7
  • 1
    What do you want to get? All objects that contain the searched word and their parents? Commented Mar 18, 2018 at 20:17
  • You want to get tiger shark and all parrents? Commented Mar 18, 2018 at 20:17
  • And why do you name the keys "Object1List", "Object2List" instead of just children ? Commented Mar 18, 2018 at 20:20
  • Hi Jonas W and Artur. Yes, I want to get all the objects that contain the searched word and their parents Commented Mar 18, 2018 at 20:33
  • Hi Jonas, I don't know. Call it as you want, it would also be valid children1list and children2list Commented Mar 18, 2018 at 20:36

2 Answers 2

1

You could check each property which is an array and take only filtered values.

This approach mutates the original array.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Nina, your function is valid, thank you very much.
0

I assume that every of your objects has this structure:

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

So then it is quite easy to filter recursively:

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

Usable as:

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

1 Comment

Thank you very much for your response, but the name of the id is not the same, so it can not be recursive. In the example I'm wrong and I name all identical ids, I've changed it now (In the Object4List the id is name id_client now) I am so sorry by the mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.