Search code examples
javascriptarraysjsonobject

Find objects of a certain length inside the object


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

Sample Test Object

let objTest = {
    "id": "66100c8e58d0a40e97ce7753",
    "createdBy": "crm1",
    "attributes": {
        "accountsInfoMessages": [{
            "valueOzb": "Mijozning holati yoritilgan. Hisob yaratish mumkin emas. Mijoz ma'lumotlarini to'ldiring. Buning uchun \"Kartochkani tanrirlash\" tugmasini bosing.",
            "code": "notCreateAccount",
        }, {
            "valueOzb": "Mijozning holati yoritilgan. Hisob yaratish mumkin emas. Mijoz ma'lumotlarini to'ldiring. Buning uchun \"Kartochkani tanrirlash\" tugmasini bosing.",
            "code": "notCreateAccount",
        }, {
            "valueOzb": "Mijozning holati yoritilgan. Hisob yaratish mumkin emas. Mijoz ma'lumotlarini to'ldiring. Buning uchun \"Kartochkani tanrirlash\" tugmasini bosing.",
            "code": "notCreateAccount",
        }],
        "selectedCardProduct": null,
        "homePageId": null,
        "branch": null,
        "customerId": "1111",
        "selectForCurrencyTest": "111",
        "selectForFiltersCardType": {
            "values": [{
                "code": "01",
                "valueOzb": "HUMO"
            }, {
                "code": "02",
                "valueOzb": "UzCard",
            }, {
                "code": "03",
                "valueOzb": "Visa"
            }],
        },
        "selectForCurrency": [{
            "valueOzb": "Andor pesetasi",
            "code": "ADP"
        }, {
            "code": "AED",
            "valueOzb": "Birlashgan Arab Amirliklari dirhami"
        }, {
            "valueOzb": "Afg'ani",
            "code": "AFA"
        }, ]
    },
    "selectForCurrency": "Test"
}

Solution

  • 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,
      nullVal: null,
      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 (!val) continue;
        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