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 6215313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:01:59+00:00 2026-05-24T07:01:59+00:00

I have a simple array: var arr = [‘has_cats’, ‘has_dogs’]; and an object: var

  • 0

I have a simple array:

var arr = ['has_cats', 'has_dogs'];

and an object:

var obj = new Object();

and from the array I want to set object attributes:

for( i=0; i < arr.length; i++ ) {
        if(!arr.hasOwnProperty(arr[i])) {
                // set the object property
        }
}

After looping I should be able to call obj.has_cats but I can’t seem to find a proper way to do it in javascript. In python I would call setattr(obj,arr[i], value). I figured that if objects have a hasOwnProperty they should also have a getOwnProperty and a setOwnProperty.

Any guidance?

  • 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-24T07:02:01+00:00Added an answer on May 24, 2026 at 7:02 am

    “I figured that if objects have a hasOwnProperty they should also have
    a getOwnProperty and a setOwnProperty“

    The hasOwnProperty() function tells you whether the named property exists as a direct property of the object, as compared to being an inherited property from somewhere in the object’s prototype chain. The in operator – used like if (someProperty in someObject) {} – will tell you whether the object has that property anywhere in the prototype chain.

    You don’t need a corresponding setOwnProperty() function because you can just say:

    someObject[someProperty] = someValue;
    

    I guess the idea of a corresponding getOwnProperty() function kind of makes sense if you want a function that only returns a value if the specified property is a direct property, but then there wouldn’t be any way to indicate that the specified property wasn’t found because null, undefined, false, etc. are all legitimate potential values if the property is found. So to achieve that you need to do it as a two-step process using if (hasOwnProperty()).

    But it doesn’t sound like that’s what you’re trying to do. If I understand you correctly, you just want some way to set a property where the property name is in a variable (in your case, an array element). You don’t make it clear what values you want associated to those properties, so I’ll just use true.

    var arr = ['has_cats', 'has_dogs'];
    
    var obj = {}; // note {} is generally preferred to new Object();
    
    for(var i=0; i < arr.length; i++ ) {
      // if the property doesn't already exist directly on
      // the object
      if(!obj.hasOwnProperty(arr[i])) {
         // set the object property
         obj[arr[i]] = true;
      }
    }
    
    // can access with bracket notation
    alert(obj["has_cats"]);
    
    // can access with dot notation
    alert(obj.has_cats);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple array: var RedirUrl = new Array(4); RedirUrl[0] = 'http://mafi.se/mf_redir/new_install_'+this_version+'.html'; RedirUrl[1]
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]
I have a very simple array (please focus on the object with "points.bean.pointsBase" as
I'm new to javascript and have a simple task. I have an array values
i have a simple array and i want to generate string which include all
I have a simple page that loads a random image from an array and
Hi I have this simple array inside my variable with some colors var clr
I have a simple actionscript function var string:String = TEXT REMOVED; var myArray:Array =
I have this very simple array which I want to be able to move
I have some simple jQuery code: var response = Array() $('.task-list').each(function() { response.concat(response,$('#' +

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.