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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:35:02+00:00 2026-05-29T12:35:02+00:00

I was trying to stringify an array-like object that was declared as an array

  • 0

I was trying to stringify an array-like object that was declared as an array object and found that JSON.stringify wasn’t processing correctly array-like object when it is defined as an array object.

See below for more clarity, –> jsFiddle

var simpleArray = []; //note that it is defined as Array Object 

alert(typeof simpleArray); // returns object -> Array Object

simpleArray ['test1'] = 'test 1';
simpleArray ['test2'] = 'test 2';

alert(JSON.stringify(simpleArray)); //returns [] 

It worked fine and returned me {"test1":"test 1","test2":"test 2"} when I changed

var simpleArray = []; to var simpleArray = {};.

Can someone shed some light or some reference where I can read more?

Edit:

Question: When typeof simpleArray = [] and simpleArray = {} returned object, why JSON.stringify wasn’t able to return {"test1":"test 1","test2":"test 2"} in both cases?

  • 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-29T12:35:05+00:00Added an answer on May 29, 2026 at 12:35 pm

    You don’t want an array. When you want an “associative array” in JavaScript, you really want an object, {}.

    You can distinguish them with instanceof Array:

    [] instanceof Array // true
    ({}) instanceof Array // false
    

    EDIT: it can process it. It serializes all of the elements of the array. However, to be an element, there must be a numeric key. In this case, there are none.

    This is nothing unique to JSON. It’s consistent with toSource and the length property.

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

Sidebar

Related Questions

When trying to stringify an object that holds an array of objects, I'm getting
I'm trying to pass an array to json.stringify but the returned value is coming
I've been trying to stringify a javascript array which is keyed by strings. JSON
I am simply trying to see the Ti.Contacts.Person object in my log so that
I'm trying to stringify a multi-array variable into a JSON string in Javascript. The
I'm working on a Firefox extension and I'm trying to stringify a JSON object.
I am trying to write json string that I stringified using https://github.com/douglascrockford/JSON-js/blob/master/json2.js ( JSON.stringify)
I'm trying convert jQuery Data object to JSON string. I tried: $.data(plant).toJSON() JSON.stringify($.data(plant)) but
Trying to honor a feature request from our customers, I'd like that my application,
I've got a JSON value that has been converted from a JavaScript object using

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.