JS获取多维数组中相同键的值实现方法示例
南来北往的客 人气:0本文实例讲述了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程序设计有所帮助。
加载全部内容