Jomol John Jomol John - 21 days ago 6
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.

Array[2]
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

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