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

  • Home
  • SEARCH
  • 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 4558864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:42:29+00:00 2026-05-21T17:42:29+00:00

I have two objects: object1={ type: ‘obj1’, nName: ‘nName’ } object2={ type: ‘obj2’, pName:

  • 0

I have two objects:

object1={
   type: 'obj1',
   nName: 'nName'
}

object2={
   type: 'obj2',
   pName: 'pName'
}

In my js code, I have:

object=GET_OBJECT();

The GET_OBJECT() method returns either object1 or object2, then, I would like to access the object’s name attribute which is either nName or pName.

I have one method which will get the name (pName or nName) of the returned object:

function getName(Object, name){
      return object.name;
}

where I would like the name to be a variable, so that I can access the pName or nName in this way:

object=GET_OBJECT();

var name='';

if(object.type=='obj1')
   name='nName';
else
   name='pName';

var finalName=getName(object, name);

But seems it won’t work since in:

function getName(Object, name){
          return object.name;
    }

name is a variable. In JS, is there any way to access attribute as a variable?

  • 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-21T17:42:30+00:00Added an answer on May 21, 2026 at 5:42 pm

    Try like this:

    function getName(Object, name) {
        return Object[name];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have Two Objects: object1, object2 When I do the following assign: object2.Foo
Suppose I have a design like this: Object GUI has two objects: object aManager
I have two objects of the same type, and I want to loop through
I have two Generic Lists with the same objects Type T within them. For
I have two objects, and one inherites from the other. the parent object sends
My method receives two parameters, both of Object type. They have the same type,
I have two collections of objects of different type. Lets call them type ALPHA
I have two objects both of the same type, for instance: typedef struct s_Object
I have two objects, let's call them Input and Output Input has properties Input_ID
If I have two objects, one being the list of items, and the other

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.