associative-array

Efficient syntax for populating a javascript associative array

十年热恋 提交于 2019-11-30 13:00:19
问题 I have an autocomplete text box that users can type an item code into and need to find out what the id number of that item code is in javascript. An associative array is the way I would imagine it should be done, but the following seems a little long winded and I'm hoping someone has a better way to do it or shorthand of what I have below: var itemIds = new Array(); itemIds["item1"] = 15; itemIds["item2"] = 40; itemIds["item3"] = 72; ... function getItemId(code){ return itemIds[code]; } 回答1:

Bash indirect reference to an associative array

丶灬走出姿态 提交于 2019-11-30 08:45:20
问题 In this very simplified example, I need to address both key and value of an array element: declare -A writer writer[H.P.]=Lovecraft writer[Stephen]=King writer[Clive]=Barker writer[Jack]=Ketchum for i in ${!writer[@]} do echo "$i ${writer[$i]}" done fullname() { pointer=$1[@] for i in "${!pointer}" do echo "? $i" done } fullname writer The function must display the output in the same format as the example loop before it, and it should receive either array name, list of keys or values, all of

Doing a “Diff” on an Associative Array in javascript / jQuery?

血红的双手。 提交于 2019-11-30 08:37:42
问题 If I have two associative arrays, what would be the most efficient way of doing a diff against their values? For example, given: array1 = { foreground: 'red', shape: 'circle', background: 'yellow' }; array2 = { foreground: 'red', shape: 'square', angle: '90', background: 'yellow' }; How would I check one against the other, such that the items missing or additional are the resulting array. In this case, if I wanted to compare array1 within array2, it would return: array3 = {shape: 'circle'}

Does powershell have associative arrays?

我的未来我决定 提交于 2019-11-30 07:21:27
问题 I am writing a function that returns an id, name pair. I would like to do something like $a = get-name-id-pair() $a.Id $a.Name like is possible in javascript. Or at least $a = get-name-id-pair() $a["id"] $a["name"] like is possible in php. Can I do that with powershell? 回答1: also $a = @{'foo'='bar'} or $a = @{} $a.foo = 'bar' 回答2: Yes. Use the following syntax to create them $a = @{} $a["foo"] = "bar" 回答3: Will add also the way to iterate through hashtable, as I was looking for the solution

How to rename an associative array in Bash?

ぃ、小莉子 提交于 2019-11-30 06:38:24
问题 I need to loop over an associative array and drain the contents of it to a temp array (and perform some update to the value). The leftover contents of the first array should then be discarded and i want to assign the temp array to the original array variable. Sudo code: declare -A MAINARRAY declare -A TEMPARRAY ... populate ${MAINARRAY[...]} ... while something; do #Drain some values from MAINARRAY to TEMPARRAY ${TEMPARRAY["$name"]}=((${MAINARRAY["$name"]} + $somevalue)) done ... other

Fastest way to implode an associative array with keys

◇◆丶佛笑我妖孽 提交于 2019-11-30 05:55:30
问题 I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use ' & ' for xhtml links or ' & ' otherwise. My first inclination is to use foreach but since my method could be called many times in one request I fear it might be too slow. <?php $Amp = $IsXhtml ? '&' : '&'; $Parameters = array('Action' => 'ShowList', 'Page' => '2'); $QueryString = ''; foreach ($Parameters as $Key => $Value)

How are javascript arrays implemented?

你说的曾经没有我的故事 提交于 2019-11-30 05:52:24
Namely, how does the following code: var sup = new Array(5); sup[0] = 'z3ero'; sup[1] = 'o3ne'; sup[4] = 'f3our'; document.write(sup.length + "<br />"); output '5' for the length, when all you've done is set various elements? My 'problem' with this code is that I don't understand how length changes without calling a getLength() or a setLength() method. When I do any of the following: a.length a['length'] a.length = 4 a['length'] = 5 on a non-array object, it behaves like a dict / associative array. When I do this on the array object, it has special meaning. What mechanism in JavaScript allows

Efficient syntax for populating a javascript associative array

安稳与你 提交于 2019-11-30 05:00:29
I have an autocomplete text box that users can type an item code into and need to find out what the id number of that item code is in javascript. An associative array is the way I would imagine it should be done, but the following seems a little long winded and I'm hoping someone has a better way to do it or shorthand of what I have below: var itemIds = new Array(); itemIds["item1"] = 15; itemIds["item2"] = 40; itemIds["item3"] = 72; ... function getItemId(code){ return itemIds[code]; } What you're doing isn't an array - it's an object (objects in JavaScript are the equivalent-ish of

Javascript: Checking if an object has no properties or if a map/associative-array is empty [duplicate]

旧时模样 提交于 2019-11-30 04:16:46
Possible Duplicate: How do I test for an empty Javascript object from JSON? Is there an easy way to check if an object has no properties, in Javascript? Or in other words, an easy way to check if a map/associative array is empty? For example, let's say you had the following: var nothingHere = {}; var somethingHere = {foo: "bar"}; Is there an easy way to tell which one is "empty"? The only thing I can think of is something like this: function isEmpty(map) { var empty = true; for(var key in map) { empty = false; break; } return empty; } Is there a better way (like a native property/function or

Slicing a multi-dimensional PHP array across one of its elements

若如初见. 提交于 2019-11-30 03:09:13
Say for example you just queried a database and you recieved this 2D array. $results = array( array('id' => 1, 'name' => 'red' , 'spin' => 1), array('id' => 2, 'name' => 'green', 'spin' => -1), array('id' => 3, 'name' => 'blue' , 'spin' => .5) ); I often find myself writing loops like this. foreach($results as $result) $names[] = $result['name']; My questions is does there exist a way to get this array $names without using a loop? Using callback functions count as using a loop. Here is a more generic example of getting every field. foreach($results as $result) foreach($result as $key => $value