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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:19:36+00:00 2026-06-17T02:19:36+00:00

I need to populate a json file, now I have something like this: {element:{id:10,quantity:1}}

  • 0

I need to populate a json file, now I have something like this:

{"element":{"id":10,"quantity":1}}

And I need to add another “element”. My first step is putting that json in a Object type using cart = JSON.parse, now I need to add the new element.
I supposed I must use cart.push to add another element, I tried this:

var element = {};
element.push({ id: id, quantity: quantity });
cart.push(element);

But I got error “Object has no method push” when I try to do element.push, and I think I’m doing something VERY wrong because I’m not telling the “element” anywhere.

How can I do that?

Edit: sorry to all I had a LOT of confusion in my head.

I thought I can get only object type when taking data from JSON.parse, but I get what I put in the JSON in the first place.

Putting array instead of object solved my problem, I used lots of suggestions got here too, thank you all!

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

    Your element is not an array, however your cart needs to be an array in order to support many element objects. Code example:

    var element = {}, cart = [];
    element.id = id;
    element.quantity = quantity;
    cart.push(element);
    

    If you want cart to be an array of objects in the form { element: { id: 10, quantity: 1} } then perform:

    var element = {}, cart = [];
    element.id = id;
    element.quantity = quantity;
    cart.push({element: element});
    

    JSON.stringify() was mentioned as a concern in the comment:

    >> JSON.stringify([{a: 1}, {a: 2}]) 
          "[{"a":1},{"a":2}]" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this form, in which i need to populate a combo box with
I have a form that I need to populate with JSON data. The form
I have a JSON file that will create my ParentModel as well as populate
I ve data in the JSON format given below,need to populate the slick grid
I have an html form and I need to populate one of the input
I have a livesearch script that i need to populate my html menu with
I have a large XML file (converted to JSON) that consists of multiple repeating
I need to dynamically populate a table footer from a JSON data source. Here
I need to make multiple NSURLConnections to a JSON Web Service. I would like
I am receiving content from a JSON call and I would like to populate

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.