-4

I wasn't sure what to call it for the title,

How can I create an array like this in JavaScript?

var per_kg_list = new Array(
    '03749' => '3.000',
    '03750' => '4.000',
    '03751' => '5.000',
);
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Nicekiwi
  • 4,567
  • 11
  • 49
  • 88
  • basically you are looking for key-value pairs in javascript, you can refer this answer for that http://stackoverflow.com/questions/7196212/how-to-create-dictionary-and-add-key-value-pairs-dynamically-in-javascript – AurA Aug 21 '13 at 05:19
  • 4
    Take a tour of the basics here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Object_literals – elclanrs Aug 21 '13 at 05:20
  • The canonical is *[How to do associative array/hashing in JavaScript](https://stackoverflow.com/questions/1208222)*. – Peter Mortensen Jul 19 '20 at 20:58

4 Answers4

3

You can have an array of objects containing key-value pairs, like this:

var per_kg_list = [
    {'03749': '3.000'},
    {'03750': '4.000'},
    {'03751': '5.000'}
];

Alternatively, you can have an object containing key-value pairs, like this:

var per_kg_list = {
    '03749': '3.000',
    '03750': '4.000',
    '03751': '5.000'
};
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
0

In JavaScript, array doesn't allow to use string as indexes. You may consider to use dictionary:

var per_kg_list = {};
per_kg_list['03749'] = '3.000';
per_kg_list['03750'] = '4.000';
per_kg_list['03751'] = '5.000';
zs2020
  • 53,766
  • 29
  • 154
  • 219
0

Someone answered the best way to initializing a JavaScript array with values:

var per_kg_list = {'03749': '3.000', '03750': '4.000', '03751': '5.000'};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54
0

You can use associative array notations. They are just JavaScript objects with properties:

var set = new Object();
set['Document language'] = 'HTML5';
set['Styling language'] = 'CSS3';
set['JavaScript library'] = 'jQuery';
set['Other'] = 'Usability and accessibility';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • They are not equivalent – Ruan Mendes Aug 21 '13 at 06:00
  • @JuanMendes Okay .. I am just learning JavaScript and this is my first JavaScript answer .. can u pls let me know where is the issue ? – Amarnath Aug 21 '13 at 06:07
  • Array extends Object with methods like push, pop, and you would give it numeric indices. If you don't need that behavior, you should just use object.http://www.phabricator.com/docs/phabricator/article/Javascript_Object_and_Array.html – Ruan Mendes Aug 21 '13 at 07:49
  • The comment about the longhand for maps is wrong in the article you linked to, not sure if it's a typo – Ruan Mendes Aug 21 '13 at 07:54
  • If you just change your code to say `=new Object ()` or `={}`, then it would be correct. Your code will work but you should understand the difference between object and arrays – Ruan Mendes Aug 21 '13 at 07:58
  • 1
    Here's a link from Google's style guide, explaining why you shouldn't use arrays as associative maps http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Associative_Arrays#Associative_Arrays – Ruan Mendes Aug 23 '13 at 19:46