Home > Web Front-end > JS Tutorial > JS example of how to get the value of the same key in a multidimensional array

JS example of how to get the value of the same key in a multidimensional array

高洛峰
Release: 2017-01-14 10:56:41
Original
1405 people have browsed it

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=[{&#39;key1&#39;:&#39;value1&#39;,&#39;key2&#39;:&#39;value2&#39;,&#39;key3&#39;:&#39;value3&#39;},{&#39;key1&#39;:&#39;value4&#39;,&#39;key2&#39;:&#39;value5&#39;,&#39;key3&#39;:&#39;value6&#39;},{&#39;key1&#39;:&#39;value7&#39;,&#39;key2&#39;:&#39;value8&#39;,&#39;key3&#39;:&#39;value9&#39;}] ;
  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("({&#39;"+i+"&#39;:&#39;"+obj[i]+"&#39;})"));
  }
  //alert(narr[0].key1);
  console.log(narr[1].key2);
}
jstest();
</script>
</HEAD>
<BODY>
</BODY>
</HTML>
Copy after login

The operation effect diagram is as follows:

JS example of how to get the value of the same key in a multidimensional array

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template