Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:48:12+00:00 2026-05-30T13:48:12+00:00

I have a global object MyFruits that can have multiple properties. At a minimum,

  • 0

I have a global object MyFruits that can have multiple properties. At a minimum, it has two properties: Apples and Oranges. I need to find out if it has more properties than just these two.

For the moment, I stringify MyFruits, create a new object with just the Apples and Oranges properties and stringify it too and compare the length of both strings. Something like this:

var JsonString1 = JSON.stringify(MyFruits);

var test2 = new Object();
test2.Apples= MyFruits.Apples;
test2.Oranges= MyFruits.Oranges;

var JsonString2= JSON.stringify(test2);

alert(JsonString1.length - JsonString2.length);

For now, it initially seems like it’s working: if MyFruits contains more properties than just the two main ones, the difference of length will be different from 0.

There’s the Object.keys(obj).length method that can count the number of properties of an object but it’s new and not supported in all browsers so I’m not going to use it.

Is this the best way to do it? Let me know if there’s a better way.

Thanks for your suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:48:14+00:00Added an answer on May 30, 2026 at 1:48 pm

    Instead of serializing the object, you’d better loop through the object, and check if there’s any property which is not Apples or Oranges:

    for (var i in MyFruits) {
        if (MyFruits.hasOwnProperty(i) && i !== 'Apples' && i !== 'Oranges') {
            throw 'Not expected!'; // Do something, eg: error_flag=true;break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a global JavaScript object with multiple properties and functions that I am
I have a global object in my main controllers that I need to pass
In my senario, I have a global setting object, say GlobalSettings, it has a
I have a recursion function that parses an object/array with a global variable. If
I have a global object X and a class A. I need a function
I have a javascript file with a global object that must not be renamed
I have some properties in an object that I would like to add to
Say I want to have a global object which can be visible and accessible
I have a custom security principal object which I set in the global.asax for
You can have different naming convention for class members, static objects, global objects, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.