Questions tagged [associative-array]

An associative array is an abstract data type composed of a collection of unique keys mapped to a collection of values.

An associative array (also associative container, map, mapping, dictionary, finite map, and in query-processing an index or index file) is an abstract data type composed of a collection of unique keys and a collection of values, where each key is associated with one value (or set of values). The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by an associative array. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key "bob" is 7, we say that our array maps "bob" to 7. Associative arrays are very closely related to the mathematical concept of a function with a finite domain. As a consequence, a common and important use of associative arrays is in memoization.

See also

3174 questions
13
votes
4 answers

Can you convert C# dictionary to Javascript associative array using asp.net mvc Json()

I recently asked this question, but after some of the responses and some research, i wanted to change what i was actually asking. i have seen a number of blog posts about sending associative arrays from javascript to C# controller action but i want…
leora
  • 188,729
  • 360
  • 878
  • 1,366
13
votes
10 answers

Generate a nested object structure by string key/path

I want to make a function called createAssociativeArray which will recive two parameters: string and object, like this: function createAssociativeArray(string, object) { //... } The last item of string should get the object data. See an…
Caio Tarifa
  • 5,973
  • 11
  • 46
  • 73
13
votes
9 answers

PHP Implode Associative Array

So I'm trying to create a function that generates a SQL query string based on a multi dimensional array. Example: function createQueryString($arrayToSelect, $table, $conditionalArray) { $queryStr = "SELECT ".implode(", ", $arrayToSelect)." FROM…
st4ck0v3rfl0w
  • 6,665
  • 15
  • 45
  • 48
13
votes
3 answers

Creating an associative array in JavaScript using the map function

I've an array of objects with the following format [{'list': 'one', 'item': 1}, {'list': 'one', 'item': 2}, {'list': 'one', 'item': 3}, {'list': 'two', 'item': 1}, {'list': 'two', 'item': 2}] And I want to transform it like this [{'one': [1, 2,…
13
votes
7 answers

How to combine associative arrays in bash?

Does anyone know of an elegant way to combine two associative arrays in bash just like you would a normal array? Here's what I'm talking about: In bash you can combine two normal arrays as follows: declare -ar array1=( 5 10 15 ) declare -ar array2=(…
Benjamin Leinweber
  • 2,774
  • 1
  • 24
  • 41
13
votes
3 answers

nested associative arrays in bash

Can one construct an associative array whose elements contain arrays in bash? For instance, suppose one has the following arrays: a=(a aa) b=(b bb bbb) c=(c cc ccc cccc) Can one create an associate array to access these variables? For…
user001
  • 1,850
  • 4
  • 27
  • 42
13
votes
6 answers

How do you remove a value that has an empty key from an associative array in PHP?

I have a key that appears to be an empty string, however using unset($array[""]); does not remove the key/value pair. I don't see another function that does what I want, so I'm guessing it's more complicated that just calling a function. The line…
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
13
votes
6 answers

Getting the key of the only element in a PHP array

The key of the associative array is dynamically generated. How do I get the "Key" of such an array? $arr = array ('dynamic_key' => 'Value'); I am aware that It is possible to access it through a foreach loop like this: foreach ($arr as $key =>…
shxfee
  • 5,188
  • 6
  • 31
  • 29
13
votes
5 answers

Use pop() with JavaScript Associative Arrays

How can I do something like the following in JS? I would like to imitate .pop() on an object rather than an array. var deck = { 'cardK' :'13', 'cardQ' :'12', 'cardAJ':'11' }; var val = deck.pop(); console.log("Key" + val.key );…
Il Profeta Profeta
  • 312
  • 1
  • 4
  • 17
13
votes
7 answers

How to POST an associative array in PHP

I have the following form:
user1134475
  • 415
  • 1
  • 8
  • 18
13
votes
1 answer

php compare two associative arrays

i have these two associative arrays // the needle array $a = array( "who" => "you", "what" => "thing", "where" => "place", "when" => "hour" ); // the haystack array $b = array( "when" => "time", "where" => "place", "who" => "you", "what" =>…
Julian Paolo Dayag
  • 3,562
  • 3
  • 20
  • 32
12
votes
7 answers

How to implement associative array (not dictionary) in Python?

I trying to print out a dictionary in Python: Dictionary = {"Forename":"Paul","Surname":"Dinh"} for Key,Value in Dictionary.iteritems(): print Key,"=",Value Although the item "Forename" is listed first, but dictionaries in Python seem to be…
jondinham
  • 8,271
  • 17
  • 80
  • 137
12
votes
6 answers

How to loop through an JSON associative array in javascript?

I'm getting a JSON response from the server and i have to loop through the array in javascript and get the values. But I cant seem to loop throught it. The JSON response of the array looks like this: {    "1": "Schools",    "20":…
ssin
  • 221
  • 1
  • 4
  • 13
12
votes
3 answers

Why do associative arrays don't work in localStorage[""]?

For example I have the following code: localStorage["screenshots"] = new Array(); localStorage["screenshots"]["a"] = 9; alert(localStorage["screenshots"]["a"]); Arr = new Array(); Arr["screenshots"] = new Array(); …
ComFreek
  • 29,044
  • 18
  • 104
  • 156
12
votes
3 answers

Set key dynamically inside map() in javascript?

So I know how to set the key dynamically like this: var hashObj = {}; hashObj[someValue] = otherValue; But I haven't seen any answer regarding map(): var list = ['a', 'b', 'c']; var hashObject = list.map(function(someValue) { return {…
rublex
  • 1,893
  • 5
  • 27
  • 45