The example of this article describes the JS implementation method of obtaining the value of the same key in a multi-dimensional array. Share it with everyone for your reference, the details are as follows:
<!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>
The operation effect diagram is as follows:
Hope What this article describes will be helpful to everyone in JavaScript programming.
For more JS implementation methods for obtaining the values of the same key in a multi-dimensional array, please pay attention to the PHP Chinese website for related articles!