{"id":407,"date":"2014-04-04T09:40:37","date_gmt":"2014-04-04T01:40:37","guid":{"rendered":"http:\/\/carlos.aboutme.com.hk\/blog\/?p=407"},"modified":"2014-06-26T09:20:07","modified_gmt":"2014-06-26T01:20:07","slug":"javascript-array-with-named-index","status":"publish","type":"post","link":"https:\/\/carlos.aboutmy.info\/blog\/?p=407","title":{"rendered":"JavaScript array with named index"},"content":{"rendered":"<p>JavaScript \u5176\u4e2d\u4e00\u500b Magic \u7684\u5730\u65b9\u5c31\u662f array \u53ef\u4ee5\u6709 named index\uff0c\u5373\u662f<br \/>\n<code><br \/>\nvar arr = [];<br \/>\narr['blue'] = 1;<br \/>\narr['red'] = 2;<br \/>\n<\/code><br \/>\n\u4f46\u7576 JSON.stringify(arr) \u6642\uff0c\u53ea\u6703\u51fa\u73fe []\u3002\u5982\u679c\u771f\u7684\u8981 stringify\uff0c\u90a3\u9ebc\u5c31\u53ea\u80fd\u53ea\u7528 map\uff0c\u5373\u662f\uff1a<br \/>\n<code><br \/>\nvar arr = {}; \/\/ \u6216\u76f4\u63a5 var arr = { blue:1, red: 2};<br \/>\narr.blue = 1;<br \/>\narr.red = 2;<br \/>\n<\/code><br \/>\n\u7528 map \u90fd\u53ef\u4ee5\u7576\u6210 array \u7528\uff0c<br \/>\n<code><br \/>\nvar keys = Object.keys(arr);<br \/>\n<\/code><br \/>\n\u5c31\u53ef iterate keys \u4e86<br \/>\n<code><br \/>\nkeys.forEach(function(o, i){<br \/>\n    console.log(o + ': ' + arr[o]);<br \/>\n});<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u5176\u4e2d\u4e00\u500b Magic \u7684\u5730\u65b9\u5c31\u662f array \u53ef\u4ee5\u6709 named index\uff0c\u5373\u662f var arr = []; arr[&#8216;blue&#8217;] = 1; arr[&#8216;red&#8217;] = 2; \u4f46\u7576 JSON.stringify(arr) \u6642\uff0c\u53ea\u6703\u51fa\u73fe []\u3002\u5982\u679c\u771f\u7684\u8981 stringify\uff0c\u90a3\u9ebc\u5c31\u53ea\u80fd\u53ea\u7528 map\uff0c\u5373\u662f\uff1a var arr = {}; \/\/ \u6216\u76f4\u63a5 var arr = { blue:1, red: 2}; arr.blue = 1; arr.red = 2; \u7528 map \u90fd\u53ef\u4ee5\u7576\u6210 array \u7528\uff0c var keys = Object.keys(arr); \u5c31\u53ef iterate keys\u2026 <span class=\"read-more\"><a href=\"https:\/\/carlos.aboutmy.info\/blog\/?p=407\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/407"}],"collection":[{"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=407"}],"version-history":[{"count":2,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions"}],"predecessor-version":[{"id":408,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions\/408"}],"wp:attachment":[{"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/carlos.aboutmy.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}