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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:00:24+00:00 2026-06-11T02:00:24+00:00

I want to make JSON array using jQuery and got some problem: <html> <head>

  • 0

I want to make JSON array using jQuery and got some problem:

<html>
    <head>
        <title>Demo - Push Data Object</title>
        <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
        <script>
            var obj = new Object();

            $(document).ready(function() {
                $("#add").click(function() {
                    item('Item 1', '1000');
                    item('Item 2', '2000');
                    console.log(JSON.stringify(obj));
                });
            });

            function item(itemNo, price) {
                obj.itemNo = itemNo;
                obj.price = price;
            };
        </script>
    </head>

    <body>
        <input id="add" type="button" value="Add Object" />
    </body>
</html>

When the button add is click it will print:
{"itemNo":"Item 2","price":"2000"}.

What I want to achieve is like this one:

{
    {
        "itemNo":"Item 1",
        "price":"1000"
    }, 
    {
        "itemNo":"Item 2",
        "price":"2000"
    }
}

What I’m supposed to change in code above?

  • 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-11T02:00:26+00:00Added an answer on June 11, 2026 at 2:00 am

    Your desired output isn’t valid JSON; since you have a list of items, the outer brackets should be [ ]. The following code generates that (Demo):

    var objs = [];
    
    $(document).ready(function() {
        $("#add").click(function() {
            objs.push(item('Item 1', '1000'));
            objs.push(item('Item 2', '2000'));
            console.log(JSON.stringify(objs));
        });
    });
    
    function item(itemNo, price) {
        return { 
            itemNo : itemNo,
            price : price
        };
    };
    

    Output:

    [
        {
            "itemNo":"Item 1",
            "price":"1000"
        }, 
        {
            "itemNo":"Item 2",
            "price":"2000"
        }
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a PHP JSON data foreach, but I met some problem.
I want to make a Follow a button like Twitter Follow in JQuery, Json
I have a JSON service, where I use some photos. I want to make
I want make datetimepicker in my project. Using jquery how it is possible? I
I aam trying to GET an array from a JSON file using JQuery's ajax
I would like to know how can we make the JSON based array using
I have make some functions to retrieve data using the Github API. I have
I want to get the value of json array using .each here's where I
Hi every one actually I want to make a com.google.gson.JsonArray of data:[1,2,3,4,5].How can i
I want to make call to authenticate user and get back token in json

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.