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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:07:03+00:00 2026-05-23T09:07:03+00:00

Using javascript, how can I add to an array an element which contains fields

  • 0

Using javascript, how can I add to an array an element which contains fields (pairs of field name and field value)?
The purpose of this is that each element will later be inserted as a row to a DB, using ajax.
Just to make sure – after the array is ready I should be able to access a field this way:

shopsArray[4].shopName

Edit:
It’s working with Pointy’s answer but I still have a problem:

shopsArray.push( { shopId: 1, shopAddress: $('#newAddress' + j).val() } );

The first value is inserted fine, but the second one has a problem.
If I alert $('#newAddress' + j).val() than I get the correct value which has been inserted in the field in the webpage.
But if I alert shopsArray[lastElementNumber].shopAddress than I get undefined.

Can you see what’s the problem here?

Edit 2:
More elaborate code:

// save changes in main shop
shopsArray[0].shopName = $('#mainName').val();
shopsArray[0].shopAddress = $('#mainAddress').val();

// save secondary branches to array
for (var i=1; i<shopsArray.length; i++){
    shopsArray[i].shopName = $('#secondaryName' + i).val();
    shopsArray[i].shopAddress = $('#secondaryAddress' + i).val();
}

// save new branches to array
for (var j=1; j<=newshopsCounter; j++){

    var bName = $('#newName' + j).val();

    shopsArray.push({shopId: -1, userId: shopsArray[0].userId, shopName: bName, shopAddress: $('#newAddress' + j).val()});

    alert(bName);
    alert(shopArray[1].shopName);
    alert(shopsArray[1].shopId);
}

The first and third alerts give the correct values. The second one gives undefined.

  • 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-23T09:07:03+00:00Added an answer on May 23, 2026 at 9:07 am

    You mean something like

    shopsArray.push({ shopName: "Fred", value: "Ethel" });
    

    ?

    edit — now that I know that this is the sort of thing you want to do, I’ll clarify.

    JavaScript has an “object literal” syntax that allows objects to be created directly as values. The syntax involves a list of property names and values, with the names and values separated by a colon and each pair separated by commas. Thus:

    var anObject = { someProperty: "the value" };
    

    creates an object with one property and assigns it to the variable “anObject”. That’s effectively the same as:

    var temp = new Object();
    temp["someProperty"] = "the value";
    var anObject = temp;
    

    The “value” part of a property in an object literal can be any expression, but the property name must be either a string constant or an identifier (and in either case, it’s treated like a string constant). Thus, you can create an object with a property whose value comes from calling some function:

    var fancyObject = { "temperature": getTemperature() };
    

    Object literal expressions are values, and can be used anywhere you can use an expression, including function call arguments. Therefore, to add an object to an array, it’s possible to call the array “.push()” function and use an object literal as the argument, as in the first example:

    shopsArray.push({ shopName: "Cheese Shoppe", shopPhone: "111 222 3232" });
    

    You can even include object literals inside another object literal, as the value of a property:

    shopsArray.push({
      shopName: "Cheese Shoppe", 
      shopAddress: {
        street1: "207 High Street",
        street2: "No. 5",
        city: "Austin",
        state: "TX"
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I load an external JavaScript file using a bookmarklet? This would overcome
Can I add an object of Elements inside another DOM element using grab or
How can I convert a character to its ASCII code using JavaScript? For example:
How do you safely encode a URL using JavaScript such that it can be
Can I tell, using javascript, whether a user has clicked on the X icon
How can I change CSS from javascript . I'm using jQuery-ui Dialog and I
When using Google Chrome, I want to debug some JavaScript code. How can I
Without using Javascript, is there a way to make a CSS property toggle on
I'm using JavaScript to hide an image and show some text thats hidden under
I need to edit (using javascript) an SVG document embedded in an html page.

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.