Skip to content Skip to sidebar Skip to footer

How Many Nested Object Should I Define In Javascript?

I am new in javascript. Here I am wondering about how many nested or inner object could i define in javascript how to find first 2 nested object's key in js. i also search it on

Solution 1:

Here I am wondering about how many nested or inner object could i define in javascript

There is no specified limit.

In the case of an object initializer such as the one you've shown, eventually you'll run into some sort of limitation of a particular JavaScript engine running the code, e.g., around its parsing / processing of the initializer, but nothing defined in the specification.

If you build objects dynamically on your own, for instance like this:

// Don't run this code!var obj = {};
while (true) {
    obj.child = {};
    obj = obj.child;
}

...there's no reason to believe you'll run into anything other than a memory limit related to the number of objects you're creating (not their nesting).

how to find how many objects keys are defined in js

In one particular object, you can get an array of its own (not inherited) properties via Object.getOwnPropertyNames and Object.getOwnPropertySymbols, then take the length of the array. To find out how many properties the object has and how many the objects its properties refer to have, you'd use a recursive search — but beware of cyclic structures.

For instance:

const obj = {
  a: {
    b: {
      c: {
      }
    }
  }
};

functioncountProps(target, recursive = false) {
  const ownProperties = [
    ...Object.getOwnPropertyNames(target),
    ...Object.getOwnPropertySymbols(target)
  ];
  let count = ownProperties.length;
  if (recursive) {
    for (const key of ownProperties) {
      const value = target[key];
      if (value && typeof value === "object") {
        count += countProps(value, true);
      }
    }
  }
  return count;
}

console.log(countProps(obj, true));

Solution 2:

  1. answer for first question.

There is not any restriction for nested object. you can define nested object as per you want until your memory limit exceed.

  1. answer for second question.

here is a code to find keys from nested objects but it is work for only 2nd nested object.

for (var key in ob) {
    if (!ob.hasOwnProperty(key)) 
    continue;
    var obj = ob[key];
   help += "</br>"for (var prop in obj) {
        // skip loop if the property is from prototypeif(!obj.hasOwnProperty(prop)) continue;

        help += " ---) \""+key+" "+prop+"\"</br>"

    }
}

tell me if you got what you want.

Solution 3:

Here I am wondering about how many nested or inner object could i define in javascript?

In the specification of JS it is not defined.

You can define nested or inner object how many you want.

But: do not forget: every object in your code need place in PC memory. And if you have to many objects you browser will not work correctly.

How to find how many objects keys are defined in JS?

With following metods:

Returns an array containing the names of all of the given object's own enumerable and non-enumerable properties.

Returns an array of all symbol properties found directly upon a given object.

you will find the length of how many objects keys are defined in your object.

Post a Comment for "How Many Nested Object Should I Define In Javascript?"