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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:51:11+00:00 2026-06-10T15:51:11+00:00

I im building and array where my array key is from a variable like

  • 0

I im building and array where my array key is from a variable like this:

var art = $('#article_id').val();
var stk =  $('#stk').val();
elements ={ art : stk };
alert(elements[art]);

but i end up with this output art=>50 instead of 5123=>50

  • 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-10T15:51:12+00:00Added an answer on June 10, 2026 at 3:51 pm

    ECMAScript 2015 (aka ES6 Harmony)

    ES 2015 provides support for this through a feature called computed property names (although the relevant section of the spec is called “Object Initializer”).

    Simply put, surround the variable (in general, any expression) with square brackets to evaluate it and use the result as a property name. In your example that would be

    elements = { [art]: stk };
    

    Original answer (targeting ES5)

    You cannot create object literals like that. You need to write

    elements = {};
    elements[art] = stk;
    

    The reason why elements = { art: stk } does not work is because it is equivalent to elements = { "art": stk } (with quotes). The two versions are equivalent in JavaScript as long as art is a legal identifier, and the second version makes it clear what’s going on.

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

Sidebar

Related Questions

I´m building a jquery animation from a multidimensional array, and in the callback of
I'm building an array of image filesnames (src) and the ID attribute (id) var
I have an array that looks like this array( 1 => array( 'id' =>
I am building an array based off comparing two other arrays. But when I
When I do something like this: $boards = Doctrine_Query::create() ->select('bds.id') ->from('board bds') ->whereIn('bds.type', $boardtype)
I am building a tree-like data structure out of associative arrays. Each key is
I would like to create an associative array in R from a string like
i am making my own class autoloader and and am building an array from
I'm building a nested array fetching from database; here's my script: while ($row_rsMaster =
I am building an array in javascript, using JSON.stringify() on it, and then setting

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.