associative-array

How are javascript arrays implemented?

别说谁变了你拦得住时间么 提交于 2019-11-29 04:59:17
问题 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

Can strings be used as an array index?

时光毁灭记忆、已成空白 提交于 2019-11-29 04:44:10
Can a string be used as array index in C? Ex: String Corresponding value "ONE" 1 "TWO" 2 "FIVE" 5 "TEN" 10 When a string in the above list is passed to the function, the function must return the corresponding value indicated above. Can this be achieved by declaring a constant array with string as index int *x; x["ONE"] = 1; x["TWO"] = 2; x["FIVE"] = 5; x["TEN"] = 5; return x["string received by the function"]; The above logic does not work as expected; is there a workaround to implement the above logic in order to have a string-indexed array? It might compile, but it won't work. It's not

Does powershell have associative arrays?

孤街醉人 提交于 2019-11-29 02:52:08
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? Doug Finke also $a = @{'foo'='bar'} or $a = @{} $a.foo = 'bar' Yes. Use the following syntax to create them $a = @{} $a["foo"] = "bar" Will add also the way to iterate through hashtable, as I was looking for the solution and did not found one... $c = @{"1"="one";"2"="two"} foreach($g in $c.Keys){write-host $c[$g]} #where key =

Foreach loop in jade (node.js template engine)

感情迁移 提交于 2019-11-29 02:17:53
问题 Ok, I am getting an associative array from node server and trying to render it in Jade. I obviously need a foreach loop, but nothing seems to work! I tried these both codes: - foreach row in rows { li= row - } and - rows.forEach(function(item)) { li= item - }) the array I am passing is called "rows". Any idea why this is not working? I am getting this error: 500 SyntaxError: Unexpected identifier and, with the second code: 500 SyntaxError: Unexpected token ) 回答1: try each item in rows li=

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

耗尽温柔 提交于 2019-11-29 02:14:28
问题 This question already has answers here : Closed 9 years ago . 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

Can list() in latest upcoming PHP work with associative arrays somehow?

不打扰是莪最后的温柔 提交于 2019-11-29 02:07:02
问题 Example: list($fruit1, $fruit2) = array('apples', 'oranges'); code above of course works ok, but code below: list($fruit1, $fruit2) = array('fruit1' => 'apples', 'fruit2' => 'oranges'); gives: Notice: Undefined offset: 1 in.... Is there any way to refer to named keys somehow with list like list('fruit1' : $fruit1) , have you seen anything like this planned for future release? 回答1: EDIT: This approach was useful back in the day (it was literally asked an answered over seven years ago), but see

Selecting a random element from a PHP associative array

佐手、 提交于 2019-11-29 01:25:47
I've got an associative array in PHP and want to select a random key/value pair out of it. Here's what I have so far: Initialize. $locations = array(); Loops through a SQL query and adds key/val pairs: array_push($locations, "'$location_id' => '$location_name'"); Later on, I select a random index of the array: $rand = array_rand($locations); Rand is just a number. So locations[$rand] gives me something like: '1' => 'Location 1' OK great, an assoc array element. At this point, I do not know the key of this assoc array, so I've tried the following things: foreach($locations[$rand] as $loc_id =>

How to write a good PHP database insert using an associative array

橙三吉。 提交于 2019-11-28 23:14:34
问题 In PHP, I want to insert into a database using data contained in a associative array of field/value pairs. Example: $_fields = array('field1'=>'value1','field2'=>'value2','field3'=>'value3'); The resulting SQL insert should look as follows: INSERT INTO table (field1,field2,field3) VALUES ('value1','value2','value3'); I have come up with the following PHP one-liner: mysql_query("INSERT INTO table (".implode(',',array_keys($_fields)).") VALUES (".implode(',',array_values($_fields)).")"); It

php array_merge associative arrays

☆樱花仙子☆ 提交于 2019-11-28 22:56:19
I'm trying to prepend an item to the beginning of an associative array. I figured the best way to do this is to use array_merge, but I'm having some odd consequences. I get the id and Name of products from a mysql database, and it gets returned as an associative array, like this (not the actual data coming back, but sample data for this question that represents what the data looks like approximately): $products = array (1 => 'Product 1', 42 => 'Product 42', 100 => 'Product 100'); this is getting sent to an html helper to create a dropdown that associates the key with the value, and the value

javascript Associate array

前提是你 提交于 2019-11-28 22:32:59
In python I could do something like myMap = {key: [value1, value2]} and then access the value2 using myMap[key][1] Can I do something like this in javascript? Well, you can do this: var myMap = { key: [ value1, value2 ] }; var array = myMap.key; // or myMap["key"] JavaScript doesn't have an "associative array" type, one that combines "map" behavior with array behavior like keeping track of the number of properties. Thus the common thing to do is use a plain object. In modern JavaScript now (2017), there's an explicit Map facility that allows keys to be of any type, not just strings as when