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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:16:46+00:00 2026-06-12T10:16:46+00:00

Possible Duplicate: Convert Javascript Array to JSON I am trying to figure out how

  • 0

Possible Duplicate:
Convert Javascript Array to JSON

I am trying to figure out how to return an array and store it in json variable as string, is that possible? [If not, how can I return all the outputs and store it json?] – If you know what I mean…

    //arr = ["a", "b", "c", "d", "ddd"]
    //largest = 3
    var generateEntryCodes = function(arr, largest) {
        var newText = ""
        for(var i=0; i < arr.length; i++) {
            if (arr[i] == null) {
                arr.splice(i, 1);
                i--;
            }
            var counts = arr[i].length != largest ? (parseInt(largest) - parseInt(arr[i].length)) : 0
            for (var z=0; z<counts;z++)
                newText += "0"
            var result = arr[i].splice( 0, 0, newText )
            newText = ""
            result = [{
                "result": result, 
                "total": result.length
            }]
        }
        return result
    }

I am try to output the above code as:

00a
00b
00c
00d
ddd

But as I flash the result json. I only get “ddd”… So I tried adding before the result = [{}] jSON code:

$("textarea").val( $("textarea").val("") + result + "\n")
//outputs:
   00a
   00b
   00c
   00d
   ddd

How can I get all the outputs from result variable and store it in JSON variable.

Problem: Code only store into JSON the last array.

  • 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-12T10:16:47+00:00Added an answer on June 12, 2026 at 10:16 am

    Generally speaking you can simply construct your JavaScript object and then serialize it to JSON like in the example below:

    var myArray = ['a','b','c'];
    var jsonString = JSON.stringify(myArray);
    

    That should work. Note that you might have to include JSON2.js in case your browser doesn’t support it natively.

    //Edit:
    That should work: http://jsbin.com/welcome/31083/edit

    Here’s the code:

    var generateEntryCodes = function(arr, largest) {
            var newText = "",
                codes = [],
                currentElement,
                result;
    
    
            for(var i=0; i < arr.length; i++) {
              currentElement = arr[i];
    
              var zerosToPrepend = largest - currentElement.length;
              for (var z = 0; z < zerosToPrepend; z++){
                    newText += "0"
              }
    
              currentElement = newText + currentElement;
              codes.push(currentElement);    
    
              newText = ""
            }
    
          result = {
            codes: codes,
            total: codes.length
          };
    
            return result
        }
    
    
    var result = generateEntryCodes(["a", "b", "c", "d", "ddd"], 3);
    console.log(result);
    

    This should return a JavaScript object that looks as follows:

    {"codes": ["00a", "00b", "00c", "00d", "ddd"], "total": 5}
    

    You can then take that result and use JSON.stringify(...) to convert it into a JSON string.

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

Sidebar

Related Questions

Possible Duplicate: JSON to javaScript array Can anybody point me out how i can
Possible Duplicate: How can I convert a string to boolean in JavaScript? I have
Possible Duplicate: How to count JavaScript array objects? If it's not a JavaScript array,
Possible Duplicate: How do you convert Byte Array to Hexadecimal String, and vice versa,
Possible Duplicate: Convert flat array [k1,v1,k2,v2] to object {k1:v1,k2:v2} in JavaScript? I want to
Possible Duplicate: Javascript date sorting by convert the string in to date format I
Possible Duplicate: How to convert string as object's field name in javascript I can
Possible Duplicate: How can I convert a string to boolean in JavaScript? Hi, How
Possible Duplicate: Javascript: strip out non-numeric characters from string String matching is headache for
Possible Duplicate: IE only javascript error with getElementsByTagName var rows=myTable.getElementsByTagName('tr'); rows=Array.prototype.slice.call(rows,0); Does not work

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.