這是我的第一Q- &一過,所以希望這是正常的。javaqscript:二維關聯數組101
正如有人誰通常拿起東西了很快,我發現了關於這一主題的資料是零星的,通常過於複雜,很多人說這根本不可能做到。所以這裏很簡單。
拿這個場景爲例,我們有一些形式的組件(文本框,按鈕等),都帶有一些屬性,它們都具有價值......我們希望將這些存儲一個JavaScript數組。
這是我的第一Q- &一過,所以希望這是正常的。javaqscript:二維關聯數組101
正如有人誰通常拿起東西了很快,我發現了關於這一主題的資料是零星的,通常過於複雜,很多人說這根本不可能做到。所以這裏很簡單。
拿這個場景爲例,我們有一些形式的組件(文本框,按鈕等),都帶有一些屬性,它們都具有價值......我們希望將這些存儲一個JavaScript數組。
這裏是我的tinkerings。此代碼並沒有明確回答的問題,因爲沒有任何問題被明確要求,但我希望你覺得它有用
良好的措施,這裏也是一個的jsfiddle http://jsfiddle.net/cQ8Xc/
var $parent_arr = new Array();
var $child_arr = new Array();
//we can add the key => value pairs like so:
//(obviously they won't be done like this, more likely a loop for example)
//this works just fine $child_arr[$key] = $value;
$child_arr['Top'] = '12';
$child_arr['Left'] = '13';
$child_arr['Right'] = '14';
$child_arr['Bottom'] = '15';
//we can add the array to another array like so:
$parent_arr['component1'] = $child_arr;
//clear the array for reuse (note that it is obviously not nessecary to reuse the array)
$child_arr = [];
//refill it
//note that the child arrays don't have to be identical lengths or values
$child_arr['Height'] = '22';
$child_arr['Width'] = '23';
$child_arr['Colour'] = 'blue';
$parent_arr['component2'] = $child_arr;
//we can access the data like this:
alert($parent_arr['component1']['Top']);
alert($parent_arr['component2']['Colour']);
//these didn't work for me, you've likely seen them in other answers if you've been researching this topic
//alert(JSON.stringify($parent_arr['component1'], null, 4));
//alert(JSON.stringify($parent_arr['component1']));
//alert($parent_arr['component1'].join("\n"));
//the array can be looped over like so:
for(var component in $parent_arr) {
for(var propertyName in $parent_arr[component]) {
alert(component + '.' + propertyName + '=' + $parent_arr['component1'][propertyName]);
}
}
至少你承認沒有人問過任何問題...... – 2013-05-12 10:57:58
美元前綴在所有變量上的基本原理是什麼? – Nick 2013-05-12 11:01:52
整個點是在一個簡單的形式分享信息,我本來可以去努力,並提出了假冒的問題,但有多少人將不得不在完全相同的情況?人們會根據自己的情況調整代碼,所以我想知道爲什麼不分享代碼並留下它呢? – 2013-05-12 11:03:36
爲什麼downvote?現在是不願意在互聯網上分享信息的? – 2013-05-12 10:56:56