Jomol John Jomol John - 1 year ago 107
Javascript Question

Get key and value of a java script array into variable

I have a java-script object array. When write console.log(myarry) it will show in the console in the below form.

0: Object
one: "one"

1: Object
two: "two"
length: 2

In this array my key and value are same and am trying to get the key or value to a variable and print it. When am trying the below code it showing

object object .

for (var key in myarry) {

alert("Key is " + key + ", value is" + myarry[key]);


Please help me for solving this.

Answer Source

check this snippet

var obj = [{
  "1": "one"
}, {
  "2": "two"
obj.forEach(function(item) {
  Object.keys(item).forEach(function(key) {
    console.log("key:" + key + "value:" + item[key]);

Hope it helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download