That's just what a JavaScript object is:

var myArray = {id1: 100, id2: 200, "tag with spaces": 300};
myArray.id3 = 400;
myArray["id4"] = 500;

You can loop through it using for..in loop:

for (var key in myArray) {
  console.log("key " + key + " has value " + myArray[key]);
}

var myArray = {
  id1: 100,
  id2: 200,
  "tag with spaces": 300
};
myArray.id3 = 400;
myArray["id4"] = 500;

for (var key in myArray) {
  console.log("key " + key + " has value " + myArray[key]);
}

See also: Working with objects (MDN).

In ECMAScript6 there is also Map (see the browser compatibility table there):

  • An Object has a prototype, so there are default keys in the map. This could be bypassed by using map = Object.create(null) since ES5, but was seldomly done.

  • The keys of an Object are Strings and Symbols, where they can be any value for a Map.

  • You can get the size of a Map easily while you have to manually keep track of size for an Object.

Answer from Alexey Romanov on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › keys
Array.prototype.keys() - JavaScript | MDN
The keys() method is generic. It only expects the this value to have a length property and integer-keyed properties. Unlike Object.keys(), which only includes keys that actually exist in the array, the keys() iterator doesn't ignore holes representing missing properties.
🌐
W3Schools
w3schools.com › jsref › jsref_keys.asp
JavaScript Array keys() Method
Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
🌐
Flexiple
flexiple.com › javascript › key-value-javascript
JavaScript: Obtain key-value pairs - Flexiple
Learn how to use JavaScript to obtain key-value pairs in an array efficiently. Get practical tips and examples for working with data structures.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-store-a-key-value-array-in-javascript
How to store a key=> value array in JavaScript ? - GeeksforGeeks
July 12, 2025 - In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript | MDN
Each key-value pair is an array with two elements: the first element is the property key (which is always a string), and the second element is the property value.
🌐
Reddit
reddit.com › r/typescript › how does one define an array of key value pairs where the "value" itself is an array too?
r/typescript on Reddit: How does one define an array of key value pairs where the "value" itself is an array too?
November 24, 2021 -

I want to define an array of key-value pairs where the key is a string and the value is an array of strings - something like this:

shops = [
    "shop-1": [
        "item-a",
        "item-b",
        "item-c",
    ],
    "shop-2": [
        "item-d",
        "item-e",
        "item-f",
    ],
    "shop-3": [
        "item-g",
        "item-h",
        "item-i",
    ],
]

But I'm getting a TS1005 error stating a "," is expected after all the shop name values (eg. "shop-1", "shop-2").

What am I doing wrong?

Thanks in advance!

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Create an array of key-value pair arrays from a given object - JavaScript - The freeCodeCamp Forum
November 18, 2022 - Trying to understand the code [k, obj[k], here object.keys is taking keys of an object and map them so ‘k’ refers to ‘a’ and ‘b’ which are keys. obj[k] seems we are putting mapped keys ‘k’ in an object ‘obj’ to make an array but how we are getting values in an array then?
Find elsewhere
🌐
SitePoint
sitepoint.com › javascript
Getting specific value from key. Array Object - JavaScript - SitePoint Forums | Web Development & Design Community
March 27, 2022 - I have an object: const car = [{ year: "2000", model: "500", color: "white" }, { year: "2020", model: "500", color: "white" }]; When I add the brackets around the object I can not seem to grab the year. If I remove the brackets I can access the year value by writing: var a = car.year; ...
🌐
LearnersBucket
learnersbucket.com › home › examples › javascript › how to create key value array in javascript
How to create key value array in javascript - LearnersBucket
June 12, 2019 - for(let key in obj){ if(obj.hasOwnProperty(key)){ console.log(obj[key]); } } //"Prashant Yadav" //"learnersbucket.com" //24 //["writing", "reading", "teaching"] ... Introducing frontend system design course 🔥 where you will learn to design frontend of 30+ applications. Enroll today (25% off) Sort a set in JavaScript in Ascending or Descending order
🌐
Medium
medium.com › design-bootcamp › understanding-key-value-️pairs-and-object-entries-️for-mapping-data-in-javascript-54c988bb33df
Understanding Key-Value 🗝️Pairs and Object Entries ⛩️for Mapping Data 📱in JavaScript | by Israel | Bootcamp | Medium
September 4, 2023 - Creating Key-Value Pairs: Creating ... associate them with values using colons. In arrays, each element has an index that can be considered a key, with the corresponding value....
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript key value array
How to Store Key-Value Array in JavaScript | Delft Stack
March 11, 2025 - By using the forEach method, we can easily iterate through the array and access each user’s name. This method is particularly useful for handling multiple entries and allows for efficient data manipulation and retrieval. If you need a more advanced structure for storing key-value pairs, JavaScript provides the Map object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › keys
Object.keys() - JavaScript | MDN
If you need both the property keys and values, use Object.entries() instead. ... // Basic array const arr = ["a", "b", "c"]; console.log(Object.keys(arr)); // ['0', '1', '2'] // Array-like object const obj = { 0: "a", 1: "b", 2: "c" }; console.log(Object.keys(obj)); // ['0', '1', '2'] // Array-like object with random key ordering const anObj = { 100: "a", 2: "b", 7: "c" }; console.log(Object.keys(anObj)); // ['2', '7', '100'] // getFoo is a non-enumerable property const myObj = Object.create( {}, { getFoo: { value() { return this.foo; }, }, }, ); myObj.foo = 1; console.log(Object.keys(myObj)); // ['foo']
🌐
Sentry
sentry.io › sentry answers › javascript › how can i add a key-value pair to a javascript object?
How Can I Add a Key-Value Pair to a JavaScript Object? | Sentry
You can use the spread (...) syntax to add a key-value pair to an object: ... const obj = { name: "Ben" }; const newObj = { ...obj, age: 22 }; console.log(newObj); // { name: 'Ben', age: 22 } You can spread in any iterable, such as an array, ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › keys
JavaScript Array keys() - Retrieve Array Keys | Vultr Docs
November 28, 2024 - The keys() method in JavaScript is essential for retrieving the index keys from an array. It provides a straightforward way to access these keys as an iterable, allowing you to perform operations like iterations and transformations based exclusively ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-store-a-key-value-array-in-javascript
How to store a key => value array in JavaScript?
We have used the map, object, and array.reduce() method to store the data in the key- value format in JavaScript.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map
Map - JavaScript | MDN
Groups the elements of a given iterable using the values returned by a provided callback function. The final returned Map uses the unique values from the test function as keys, which can be used to get the array of elements in each group.
🌐
Programiz
programiz.com › javascript › library › array › keys
JavaScript Array keys() (With Examples)
Become a certified JavaScript programmer. Try Programiz PRO! ... The keys() method returns a new Array Iterator object that contains the keys for each element in the array.