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

  • Home
  • SEARCH
  • 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 843765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:10:54+00:00 2026-05-15T06:10:54+00:00

I made a simple javascript class like this: function Horse() { this.color = ‘brown’;

  • 0

I made a simple javascript class like this:

function Horse() {
    this.color = 'brown';
    this.speed = 'somewhat slow';
}

I attached a few instances to some elements, like this:

$("#horse1").data('d', new Horse());
$("#horse2").data('d', new Horse());
$("#horse3").data('d', new Horse());

now I want to create a JSON array with a JSON representation of each horse object. So I’m doing this:

// How do I create an empty JSON array here?:
var myJsonArray = ?;

var children = $("#horses").children();
for (var i = 0, m = children.size(); i < m; i++) {
    var panel = children[i];
    var horse = $(panel).data('h');

    // And how do I create a JSON rep of my horse here?
    var myJsonHorse = new JsonHorse(?);

    // Finally, how do I add it to the json array?
    myJsonArray.push(myJsonHorse);
}

yeah my end goal is to have a single json array of all the horses after iterating over all the children – not sure if this should be done in plain javascript or in jquery?

Thanks

—————- Edit ——————-

Sorry, my end goal is to then convert the array to a string, so I have one large json array converted to a string that I can submit to an ajax call that will get processed by my server.

  • 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-15T06:10:55+00:00Added an answer on May 15, 2026 at 6:10 am

    To declare an array you just need the Array Literal notation:

    var myArray = [];
    

    The push method will work without problems.

    Another possibility is to use the $.map method:

    var myArray = $("#horses").children().map(function (index, el) {
      return $(el).data('d');
    }).get();
    //  Will return an array containing:
    //  [{"color":"brown","speed":"somewhat slow"},
    //   {"color":"brown","speed":"somewhat slow"},
    //   {"color":"brown","speed":"somewhat slow"}]
    

    Check an example here.

    Then to convert that array to a JSON string, you can use the JSON.stringify method (available natively on almost all browsers, for IE < 8 you can use json2.js).

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

Sidebar

Related Questions

I'm using this simple code: http://ejohn.org/blog/simple-javascript-inheritance/ Using this library, I made this simple class:
I'm using php,html,javascript,mysql. My functionality goes like this. I have a division made in
I have a JQuery, Javascript, HTML website. I made a simple function to strike
Here comes a simple question, for all those who know javascript. I made an
I made a simple game yesterday in javascript using coffeescript. The bulk of the
I have programmed a simple Jquery ticker (using a pre-made Javascript) to display tweets
I made a simple javascript translator using google API. Instead of using a button
I made a simple gallery, using this script to show the images: $rs =
BEFORE CHAOS I used to have this one... at the head part <script type=text/javascript>$(function()
I have got some markup that looks like this: <div class=button> <span class=image></span> <input

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.