<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Show all JSON elements</title>
</head>
<body>
<div id="show"></div>
<script>
jarr = JSON.parse('[{"var1":"11","var2":"12","var3":"13"},{"var1":"21","var2":"22","var3":"23","var4":"24"}]');
console.log("item 0",jarr[0]);
console.log("keys",Object.keys(jarr[0]));
console.log("values",Object.values(jarr[0]));
console.log('--------------------------');
for (i in jarr) { // The element INDEX (0..N) in the simple array
console.log("By index ",jarr[i]);
for( j in jarr[i] ) { // The key in each object
console.log(j," + ",jarr[i][j] ); // element i key j value jarr[i][j]
}
}
console.log('--------------------------');
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function myf(item,index,arr)
{
console.log("forEach ",item); // the element in the array
for(t in item)
console.log(t," - ",item[t]); // the key in the object
}
jarr.forEach(myf);
console.log('--------------------------');
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
map = {"first" : "1", "second" : "2"};
function getKeyByValue(object, value) { // find the key for a value
return Object.keys(object).find(key => object[key] == value);
}
console.log(getKeyByValue(map,"2"));
</script>
</body>
</html>