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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:57:53+00:00 2026-06-08T06:57:53+00:00

I’m using an object’s keys and values to populate other objects like so: var

  • 0

I’m using an object’s keys and values to populate other objects like so:

var default_columns = {
    column_a: 'value_a',
    column_b: 'value_b',
    column_c: 'value_c'
// [...]
}

var new_object = {};
for (var key in default_columns) {
    new_object[key] = default_columns[key];
}

But then later on in my program, I would like to resuse those keys as parameters. I could do something like this: new_object['column_a'] but if I change 'column_a' in default_columns I need to update it everywhere in the code.

I thought about defining my object like so:

var default_columns = {
    a: { k: 'column_a', v: 'value_a' },
    b: { k: 'column_b', v: 'value_b' },
    c: { k: 'column_c', v: 'value_c' }
}

and iterate through it as follows:

var new_object = {};
for (var key in default_columns) {
    new_object[default_columns[key].k] = default_columns[key].v;
}

which would also allow me to use the keys as parameters ( new_object[default_columns.a.k] ) while giving me the opportunity to change the keys (e.g. 'column_a' to 'my_column_a') in default_columns without having to update the code.

Is there a more readable way of doing what I’m trying to achieve with the 2nd approach?

  • 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-06-08T06:57:55+00:00Added an answer on June 8, 2026 at 6:57 am

    It seems to me that prototypical inheritance is what you want. Instead of copying all properties from default_columns to new_object with Object.extend, let them inherit from each other (Object.create)!

    var new_object = Object.create(default_columns);
    
    // or, in other words:
    
    function Columns(){}
    /* var default_columns = */ Columns.prototype = {
        column_a: 'value_a',
        ...
    };
    
    var new_object = new Columns();
    

    You then can overwrite some columns on the new_object, which will shadow the inherited properties.


    Although when your aim is to easily rename the properties, I’d go with the second approach. Renaming on a normal objects means two lines of code: copy to new and delete old. With a set of objects used everywhere you’d just have to change the “key” property of the objects, and it will reflect to everywhere this particular object is referenced.

    Note that your iteration to create a new object won’t reflect the changes, as k and v are dereferenced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

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.