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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:38:08+00:00 2026-05-25T02:38:08+00:00

From post: Sending a JSON array to be received as a Dictionary<string,string> , I’m

  • 0

From post:

Sending a JSON array to be received as a Dictionary<string,string>,

I’m trying to do this same thing as that post. The only issue is that I don’t know what the keys and the values are upfront. So I need to be able to dynamically add the key and value pairs and I don’t know how to do that.

How can I create that object and add key value pairs dynamically?

I’ve tried:

var vars = [{key:"key", value:"value"}];
vars[0].key = "newkey";
vars[0].value = "newvalue";

But that doesn’t work.

  • 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-25T02:38:09+00:00Added an answer on May 25, 2026 at 2:38 am

    Use:

    var dict = []; // Create an empty array
    
    dict.push({
        key:   "keyName",
        value: "the value"
    });
    // Repeat this last part as needed to add more key/value pairs
    

    Basically, you’re creating an object literal with two properties (called key and value) and inserting it (using push()) into the array.


    This does not create a "normal" JavaScript object literal (aka map, aka hash, aka dictionary).
    It is however creating the structure that OP asked for (and which is illustrated in the other question linked to), which is an array of object literals, each with key and value properties. Don’t ask me why that structure was required, but it’s the one that was asked for.

    But, but, if what you want in a plain JavaScript object – and not the structure OP asked for – see tcll’s answer, though the bracket notation is a bit cumbersome if you just have simple keys that are valid JavaScript names. You can just do this:

    // Object literal with properties
    var dict = {
      key1: "value1",
      key2: "value2"
      // etc.
    };
    

    Or use regular dot-notation to set properties after creating an object:

    // Empty object literal with properties added afterward
    var dict = {};
    dict.key1 = "value1";
    dict.key2 = "value2";
    // etc.
    

    You do want the bracket notation if you’ve got keys that have spaces in them, special characters, or things like that. E.g:

    var dict = {};
    
    // This obviously won't work
    dict.some invalid key (for multiple reasons) = "value1";
    
    // But this will
    dict["some invalid key (for multiple reasons)"] = "value1";
    

    You also want bracket notation if your keys are dynamic:

    dict[firstName + " " + lastName] = "some value";
    

    Note that keys (property names) are always strings, and non-string values will be coerced to a string when used as a key. E.g., a Date object gets converted to its string representation:

    dict[new Date] = "today's value";
    
    console.log(dict);
    // => {
    //      "Sat Nov 04 2016 16:15:31 GMT-0700 (PDT)": "today's value"
    //    }
    

    Note however that this doesn’t necessarily "just work", as many objects will have a string representation like "[object Object]" which doesn’t make for a non-unique key. So be wary of something like:

    var objA = { a: 23 },
        objB = { b: 42 };
    
    dict[objA] = "value for objA";
    dict[objB] = "value for objB";
    
    console.log(dict);
    // => { "[object Object]": "value for objB" }
    

    Despite objA and objB being completely different and unique elements, they both have the same basic string representation: "[object Object]".

    The reason Date doesn’t behave like this is that the Date prototype has a custom toString method which overrides the default string representation. And you can do the same:

    // A simple constructor with a toString prototypal method
    function Foo() {
      this.myRandomNumber = Math.random() * 1000 | 0;
    }
    
    Foo.prototype.toString = function () {
      return "Foo instance #" + this.myRandomNumber;
    };
    
    dict[new Foo] = "some value";
    
    console.log(dict);
    // => {
    //      "Foo instance #712": "some value"
    //    }
    

    (Note that since the above uses a random number, name collisions can still occur very easily. It’s just to illustrate an implementation of toString.)

    So when trying to use objects as keys, JavaScript will use the object’s own toString implementation, if any, or use the default string representation.

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

Sidebar

Related Questions

I'm trying to refresh a page without sending POST from the previous time. I've
I am having an issue trying to post a JSON string using dojo.xhrPost to
I'm trying to POST some JSON and a binary file from an iPhone to
I am facing problems in sending json response to php that I get from
i am trying to send a json string from my android client to my
I am sending post request from android to the url using httpclient like this
I've been trying to send a JSON string to PHP server like this: $.ajax({
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
say I have this $result = mysql_query('SELECT views FROM post ORDER BY views ASC');
I am trying to get some form data from POST method. Here's the code

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.