本文實例講述了JS獲取多維數組中相同鍵的值實現方法。分享給大家參考,如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Demo </TITLE> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script> function jstest(){ var arr=[{'key1':'value1','key2':'value2','key3':'value3'},{'key1':'value4','key2':'value5','key3':'value6'},{'key1':'value7','key2':'value8','key3':'value9'}] ; var obj={}; var narr=new Array(); for(var i=0;i<arr.length;i++){ for(var j in arr[i]){ if(obj[j]!=undefined) obj[j]+=arr[i][j]; else obj[j]=arr[i][j]; } } for(var i in obj){ narr.push(eval("({'"+i+"':'"+obj[i]+"'})")); } //alert(narr[0].key1); console.log(narr[1].key2); } jstest(); </script> </HEAD> <BODY> </BODY> </HTML>
運作效果圖如下:
希望本文所述對JavaScript程式設計有所幫助。
更多JS取得多維數組中相同鍵的值實現方法範例相關文章請關注PHP中文網!