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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:29:50+00:00 2026-06-18T08:29:50+00:00

I want something like this: var myObj = {}; myObj[a][b].push({c:d}); I want myObj to

  • 0

I want something like this:

var myObj = {};
myObj["a"]["b"].push({"c":"d"});

I want myObj to become: {"a":{"b":[{"c":"d"}]}} but I get an error

Cannot set property ‘b’ of undefined

Yes, I know I can create it step by step like that:

var myObj={};
myObj["a"]={};
myObj["a"]["b"]=[];
myObj["a"]["b"].push({"c":"d"});

But what if myObj already has some structure and I don’t want to destroy it. E.g:

var myObj={"a":{"e":"f"}};

if I write myObj["a"]={}; I’ll lose data.
Sure I can write

if(myObj["a"]==null){
myObj["a"]={}
}

But it’s too bulky! That would be great if I could just write smth like myObj["a"]["b"].push({"c":"d"}); without checking for null on every step.
Any suggestions how to do this? May be there is a way to override JS native Object or smth like that?
Thank you in advance!

  • 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-18T08:29:51+00:00Added an answer on June 18, 2026 at 8:29 am

    I’m afraid it’s not possible to override JS’s native functionalities like that.

    You’re going to have to check for sub-properties.

    If you want to make sure a property exists on a object, without overriding it, do something like this:

    myObj['a'] = myObj['a'] || {};
    

    This sets myObj['a'] to itself if it has a value, or to a new empty object if it doesn’t.

    Then do the same for myObj['a']['b'], and push to that:

    myObj['a']['b'] = myObj['a']['b'] || [];
    myObj['a']['b'].push({"c":"d"});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: require 'erb' @var = 'test' template =
I want to do something like $('a.filter-link').click(function(e) { e.preventDefault(); var that = $(this), categoryId
input is a string (var str) something like this: <<hello there//abcd//1234>> I want to
I want to do something like this: for(var i=0;i<aList.length;i++) { aList[i].doSomething(); sleep(500); } Of
I want a two-dimensional array of ArrayBuffer Something like this: var myRowOfStrings = new
Say I want the object to be something like this: var Book = {
I want to do something like this: var html = $(#media).html(); $(#multimedia-content).append(html); $(#multimedia-content).filter(*).removeAttr(id); The
I want to do something like this: var w = window.open(javascript: makeAnAjaxRequest();); My question
I want to do something like this: var showLi = $(<li></li>); $(showLi).append(<a href='#' class='showData'>Show</a>);
I want to do something like this: var apps = from app in context.Apps

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.