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

The Archive Base Latest Questions

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

I have a javascript object that contains a few objects that contain associative arrays.

  • 0

I have a javascript object that contains a few objects that contain associative arrays. I’ve been trying to use the json2.js library’s stringify function but the output doesn’t contain the arrays held in the contained object members.
In my code I start with something like this

obj = {"arr1" : [], "arr2" : [], "arr3" : []};

then I add to it with loops that fill each of the contained arrays

obj[arr*].push[arritem*];
obj[arr*][arritem*] = something;

The arr* and arritem* I put in just to represent the variable I am putting in for the loops.
I try Json.stringify(obj) but the string I get back is

'{"arr1" : [0], "arr2" : [0], "arr3" : [0]}'

I would like to see the ouput as

'{"arr1" : [ "arritem1" : something, "arritem2" : something2], "arr2" : [ "arritem1" : something, "arritem2" : something2], "arr3" : [ "arritem1" : something, "arritem2" : something2]}'

is there a better library for this or is there something I have to do before strinfying?

  • 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-24T06:48:11+00:00Added an answer on May 24, 2026 at 6:48 am
    var obj = {"arr1" : [], "arr2" : [], "arr3" : []};
    console.log(JSON.stringify(obj));
    

    Works for me.

    Filling the arrays works too.


    Update

    You imply that you are trying to add elements with non-numeric keys to arrays.

    This is not valid. In particular, your desired output is not valid JSON. Arrays have only numeric keys, and they are not included in the JSON itself as they are implicitly, sequentially defined.

    Arrays are a special type of Object, which handles numeric indexes for you.

    var arr = [];   // Create array.
    arr.push(1);    // There is now one element, with index 0 and value 1.
    arr["txt"] = 2; // You tried to create a new element,
                    // but didn't use .push and gave a non-numeric key.
                    // This broke your array.
    
    console.log(JSON.stringify(arr));
    // Output: [1]
    

    Live demo.

    Long story short… don’t do this. If you want an “associative array”, stick with basic objects:

    var obj    = {}; // Create object.
    obj[0]     = 1;  // There is now one element, with key "0" and value 1.
    obj["txt"] = 2;  // There is now a second element, with key "txt" and value 2.
    
    console.log(JSON.stringify(arr));
    // Output: {"0":1,"txt":2}
    

    Live demo.

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

Sidebar

Related Questions

I have a Javascript class that contains a few functions and member objects: function
I am trying to have a javascript object tree behave like a php associative
I have an javascript object that contains a select list. I'd like to loop
I have a Javascript object called TweenManager which contains an array of Tween objects.
I have a numeric javascript array, that contains several objects with geodata in it.
I have a Javascript object that requires 2 calls out to an external server
I have a Javascript object that basically represents a Row in an .NET GridView.
I have a javascript that does this (http is your XMLHttpRequest object) var r
I currently have a Javascript function that uses a string to reference an object
I'm creating my own JavaScript Array-like object and I have methods that call closures.

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.