9

I have a JavaScript 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]);
}
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Jomol MJ
  • 671
  • 1
  • 6
  • 23
  • Use `for-loop` instead of `for-in` to iterate array... Also read about `Object.keys` to get keys of `object` – Rayon Nov 17 '16 at 05:49
  • 1
    can you show me your code before the `for` where do you assign the value to `myarray`. – Teocci Nov 17 '16 at 05:49
  • Actually the array coming from a third party api and i can see the array when i write console.log(myarry) in the above format. My aim is to get the key or value into a variable. I have tried with for loop too. But it also showing object object. – Jomol MJ Nov 17 '16 at 05:52
  • It's not "java script" or "java-script"; it's "JavaScript". –  Nov 17 '16 at 06:00
  • When an object is "printed" (i.e., converted to a string), it becomes `[object Object]`. If you want to print out some reasonable representation of the object, you can use `JSON.stringify`, or do the string conversion yourself however you want. –  Nov 17 '16 at 06:02
  • sorry I was in the work so I mistaken send the answer. Now I edited and it is ok. – Teocci Nov 17 '16 at 06:32

7 Answers7

19

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

Geeky
  • 7,420
  • 2
  • 24
  • 50
4
  • Use for-loop instead of for-in to iterate array.

  • Use Object.keys to get keys of object

var arr = [{
  one: 'one'
}, {
  two: 'two'
}];

for (var i = 0, l = arr.length; i < l; i++) {
  var keys = Object.keys(arr[i]);
  for (var j = 0, k = keys.length; j < k; j++) {
    console.log("Key:" + keys[j] + "  Value:" + arr[i][keys[j]]);
  }
}
Rayon
  • 36,219
  • 4
  • 49
  • 76
2

I think you have two main options to get keys of an object using Object.keys these are: forEach; or a simple for.

1. Use forEach

If you're using an environment that supports the Array features of ES5 (directly or using a shim), you can use the new forEach:

var myarray = [{one: 'one'}, {two: 'two'}];

myarray.forEach(function(item) {
  var items = Object.keys(item);
  items.forEach(function(key) {
   console.log('this is a key-> ' + key + ' & this is its value-> ' + item[key]);
 });
});

forEach accepts an iterator function and, optionally, a value to use as this when calling that iterator function (not used above). The iterator function is called for each entry in the array, in order, skipping non-existent entries in sparse arrays. Although

forEach has the benefit that you don't have to declare indexing and value variables in the containing scope, as they're supplied as arguments to the iteration function, and so nicely scoped to just that iteration.

If you're worried about the runtime cost of making a function call for each array entry, don't be; technical details.

2. Use a simple for

Sometimes the old ways are the best:

var myarray = [{one: 'one'}, {two: 'two'}];

for (var i = 0, l = myarray.length; i < l; i++) {
  var items = myarray[i];
  var keys = Object.keys(items);
  for (var j = 0, k = keys.length; j < k; j++) {
    console.log('this is a key-> ' + keys[j] + ' & this is its value-> ' + items[keys[j]]);
  }
}
Teocci
  • 7,189
  • 1
  • 50
  • 48
2

Depending on your construction, you can do

const arr = [{ key1: 'val1' }, { key2: 'val2' }]

arr.forEach((a, i) =>
  console.log(i, [{ key: Object.keys(a) }, { val: Object.values(a) }])
)

and to answer your question

arr.forEach((a, i) =>
  alert("Key is "+ Object.keys(a) + ", value is " +  Object.values(a))
)
nvidot
  • 1,262
  • 1
  • 7
  • 20
PHANTOM-X
  • 502
  • 6
  • 16
1

am trying to get the key or value to a variable and print it.

then you could

var myarry = [{ one: 'one' }, { two: 'two' }];

for (var key in myarry) {
  var value = myarry[key];
  console.log(key, value)
}
GollyJer
  • 23,857
  • 16
  • 106
  • 174
Adr
  • 424
  • 4
  • 12
  • I have tried. I can see the value in console. But when i print, it still showing object object. Please help me – Jomol MJ Nov 17 '16 at 06:00
0

you can do it in this way

const a = [{ one: 'one' }, { two: 'two' }];

a.forEach(function(value,key) {
   console.log(value,key);
});

You can take key and value in a variable and use them.

GollyJer
  • 23,857
  • 16
  • 106
  • 174
Shikha thakur
  • 1,269
  • 13
  • 34
0

Here's an interesting way to do it.

const arr = [{ one: 'one' }, { two: 'two' }];

Object.entries(arr).forEach(([_, obj]) => {
  const key = Object.keys(obj)[0];
  console.log(`Key is ${key}, value is ${obj[key]}.`);
});
GollyJer
  • 23,857
  • 16
  • 106
  • 174