-1

i have the following Array Object :

[
   {
     "key1": abc,
     "key2":xyz
   },
   {
     "key1": abc,
     "key2":xyz
   }
]

Now what i want is to print "key1" & "key2". I know we can iterate through values using map, but i also want to iterate through Array keys.

Devs
  • 286
  • 2
  • 5
  • 16
  • There is no such thing as a "JSON Object". JSON is always a string. You have an array of objects. – Mulan Nov 02 '16 at 17:33
  • Object.keys(...) ? – NG. Nov 02 '16 at 17:34
  • Possible duplicate of [Javascript iterate object](http://stackoverflow.com/questions/14379274/javascript-iterate-object) – Mulan Nov 02 '16 at 17:35
  • @NG. Please do read the question again. I mean to print key. without having to know which keys are present. – Devs Nov 02 '16 at 17:36
  • @naomik I need to use it with reactjs, which allows to iterate the array objects using map. Please can you check if it is possible through maps. thank you. – Devs Nov 02 '16 at 17:38
  • @Devs I have no idea what you're asking but your program should never have to guess what the keys are. If the *name* of the key is data, then instead it should be the *value* of a key in your object. For example `{key: 'foo', value: 'bar'}` then you can `arr.map(x => console.log(x.key, x.value))` – to put it more plainly, if you don't know what the keys are, the shape of your state is broken and should be revised. – Mulan Nov 02 '16 at 17:41
  • given `{ code: data }`, `code` is always known to your program and `data` is free to be whatever value you need – Mulan Nov 02 '16 at 17:45
  • @naomik just so you know, programming world is very big for it to be "never have to guess" approach. I happen to have dynamic array object (where the keys will differ with each click event) values which i needed to convert into a table. So i needed the keys to be specified as headers. I have tried to pose my question in the simplest form possible for any person in its normal state to understand. Anyways i have found the solution and am going to post it. Thanks anywayz. – Devs Nov 02 '16 at 17:53
  • @Devs if you have the ability to shape the object however you want, you never have to guess. period. If this was you having to deal with (eg) an API response that was out of your control, that's a different story. – Mulan Nov 02 '16 at 17:55
  • @naomik i have gotten what i was looking for. Not going to make it any simpler for you to understand the problem. You may try to downvote it further if you like, oh! sorry you cannot. – Devs Nov 02 '16 at 18:03
  • @Devs if you treat object keys as variant data and believe you found a solution to your problem, then you've only worsened your problem. – Mulan Nov 02 '16 at 18:05
  • @naomik Not Bad, Atleast you understood what i was looking for. About worsening, just one word "StackOverFlow" . Thank you again. – Devs Nov 02 '16 at 18:11

1 Answers1

1

Assuming ArrayObj contains the key:value pairs, we can do the following:

let keys = Object.keys(ArrayObj);
for(index=0;index<keys.length;index++)
{
   console.log(keys[index]);
}
Devs
  • 286
  • 2
  • 5
  • 16