skip to Main Content

I need to go through the json object and find keys-objects, keys-arrays whose size matches a given value.

I can’t understand where to set the conditions.

function traverse(obj, size) {
    for (let key in obj) {
        if (typeof obj[key] === "object" && obj[key] != null) {
            traverse(obj[key]);
            if (Object.keys(obj[key]).length > size) {
                console.log(key);
            } else if (Array.isArray(obj[key])) {
                if (obj[key].length > size) { 
                    console.log(key);
                }
            } 
        }
    }
}

I’ve tried rearranging the if conditions, but it doesn’t give any good results

3

Answers


  1. You are calling traverse with only one argument.

    if (typeof obj[key] === "object" && obj[key] != null) {
        traverse(obj[key]); // here !
         //... rest of the code
    }
    
    

    it should be

    traverse(obj[key], size);
    

    Please privide object example with desired result to verify the logic

    Login or Signup to reply.
    1. You should pass the arguments to the recursive calls
    2. You could add an accumulator to collect the result
    3. Don’t put else if (Array.isArray(obj[key])) { where you handle an object (not an array):
    function traverse(obj, size, key = null, result = []) {
      if(Array.isArray(obj)){
        obj.forEach((item, i) => traverse(item, size, i, result));
        obj.length > size && key && result.push(key);
      } else 
      if (obj && typeof obj === "object") {
        const keys = Object.keys(obj);
        keys.forEach(key => traverse(obj[key], size, key, result));
        keys.length > size && key && result.push(key);
      }
      return result;
    }
    
    console.log(traverse({prop1: 1, prop2: [2, {prop3: 3, prop4: [1,2,3,null]}]}, 1));
    Login or Signup to reply.
  2. I’ve corrected some parts of your function as per my understanding. This should work. If it doesn’t, let me know in the comments what error/bug you encounter. Preferably, add an instance/simplified example of the object you’re traversing to your question.

    EDIT: Here’s the final draft of a tested solution:

    /* Test Data: */
    const testObject = {
      owner: 'John Doe',
      prices: [200, 450, -400, 3000, -650, -130, 70, 1300],
      interestRate: 1.2, // %
      pin: 1111,
      objOuter: {
        one: 1,
        two: 2,
        three: 3,
        arr1: [200, 450, -650],
        inner: {
          uno: 1,
          dos: 2,
          tres: 3,
          arr2: [-400, 3000],
          innermost: {
            ich: 1,
            ni: 2,
            san: 3,
            arr3: [-130, 70, 1300],
          },
        },
      },
    };
    
    /* Function: */
    function traverse(obj, size) {
      for (const [key, val] of Object.entries(obj)) {
        if (Array.isArray(val) && val.length > size) {
          console.log(key);
          val.forEach(function (el, i) {
            if (typeof el === 'object') traverse(el, size);
          });
        }
        if (typeof val === 'object' && !Array.isArray(val) && Object.keys(val).length > size) {
          console.log(key);
          traverse(val, size);
        }
      }
    }
    traverse(testObject, 3); // Function call
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search