最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Creating some associative array in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I have an object which contains an array, and I want it to contain an associative array so keys are strings. How can I do it?

This does not work:

{profiles: { easy:["example" : 1], advanced:["example" : 1] }

I wanted to avoid

console.log({profiles: { easy: {"example": 1}, advanced:{"example": 1} })

Because that easy and advanced members are not displayed as an array but as an object.

Another way I know is this:

array['key2'] = 'new value';

but that is not part of the object, so I would need to split the command for multiple lines - which is not what I want.

PHP has something like array("mykey" => "myValue"), but does JavaScript have something similar?

Yet I don't want to use frameworks. This is JavaScript only.

I have an object which contains an array, and I want it to contain an associative array so keys are strings. How can I do it?

This does not work:

{profiles: { easy:["example" : 1], advanced:["example" : 1] }

I wanted to avoid

console.log({profiles: { easy: {"example": 1}, advanced:{"example": 1} })

Because that easy and advanced members are not displayed as an array but as an object.

Another way I know is this:

array['key2'] = 'new value';

but that is not part of the object, so I would need to split the command for multiple lines - which is not what I want.

PHP has something like array("mykey" => "myValue"), but does JavaScript have something similar?

Yet I don't want to use frameworks. This is JavaScript only.

Share Improve this question edited Jul 19, 2020 at 20:57 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jul 12, 2016 at 21:59 John BoeJohn Boe 3,61110 gold badges43 silver badges78 bronze badges 4
  • AFAIK: Can't be done in JavaScript. – Script47 Commented Jul 12, 2016 at 22:00
  • 1 In Javascript, Objects are Associative Arrays – Hector Barbossa Commented Jul 12, 2016 at 22:01
  • As does @HectorBarbossa says, you will have to do this with objects : {"yourKey": 42.314} – boehm_s Commented Jul 12, 2016 at 22:05
  • The canonical is How to do associative array/hashing in JavaScript. – Peter Mortensen Commented Jul 19, 2020 at 20:57
Add a comment  | 

2 Answers 2

Reset to default 11

JavaScript doesn't have "associative arrays".

It has objects, which you can store values on in named properties.

It has arrays, which store an ordered set of values via integer properties. Since they are a type of object, you can also store named values on them (but you can't create them using the literal syntax, only add them after the array is created) but this isn't considered good practise and they will be ignored by just about anything that has special case handling for arrays.

As of ES6 it also has Maps which are similar to objects in that you can give them named values, but also to arrays in that order is preserved. There is no literal syntax for creating a Map.

wanted to avoid console.log({profiles: { easy:{"example" : 1},advanced:{"example" : 1} })

Why? Is there a reason to avoid language-specific features?

If I told you you can do this (should be avoided):

var array = [];
array["foo"] = "bar";
array.foo // -> bar

The problems with it:

array.length // -> 0

So, even if you can apply properties to mostly everything in JavaScript like:

var str = "hello";
str.type = "greetings";

you shouldn't screw the language...

This is the normal way of creating key:value maps, objects:

var obj = {foo: "bar"}
obj.foo // -> bar

Why not do it just like the language is designed to?

发布评论

评论列表(0)

  1. 暂无评论