1

I have a solution of my question, but I don't know whether it exists a better solution.

Following I had implemented:

View:

<md-list>
   <md-list-item>
      <span ng-repeat="item in ::items track by $index" flex="auto">
          {{::item}}
      </span>
      <md-divider></md-divider>
   </md-list-item>
</md-list>

Controller:

CrudSvc.GetAll().$promise.then(
  function (res) {
     $scope.items = GetKeyForTitle(res);
  },
  function (err) {
     //err code...
  }
);

function GetKeyForTitle(data) {
   var arrItems = [];
   var resData = data[0];

   angular.forEach(resData, function (val, key) {
       arrItems.push(key);
   });

   return arrItems;
}

JSON data is simple defined:

[
  {
    "NAME": "John Doe",
    "AGE": 25,
    "CITY": "New York",
    "COUNTRY": "USA"
  },...
]

For my solution I used data[0] to give me only the first data otherwise I get always the same keys and I need the keys just one time.

yuro
  • 2,189
  • 6
  • 40
  • 76
  • 1
    Possible duplicate of [Getting JavaScript object key list](http://stackoverflow.com/questions/3068534/getting-javascript-object-key-list) – Rajesh Nov 17 '16 at 08:49
  • @Rajesh Thanks for your comment, but in the thread there is no example to extract only the keys from the first data because I have 10 objects in the array that means in your case I would get ten times the same keys. – yuro Nov 17 '16 at 08:58
  • If I understand right, objective of question is to get keys of first object in an array of objects. Now you have already figured out the part of fetching first object. So all you need is a way to get keys, that mentioned post highlights – Rajesh Nov 17 '16 at 09:00
  • @Rajesh My question is, whether a better solution exists instead of using `data[0]`. How you can see I had already defined a forEach to loop through my array. – yuro Nov 17 '16 at 09:05
  • Have you tried `Array.prototype.keys()`? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys – ryanpcmcquen Nov 17 '16 at 09:18
  • @ryanpcmcquen But how you define it, that you need the keys one time only? – yuro Nov 17 '16 at 09:21
  • Nevermind, `Object.keys()` is what you want, see my answer. – ryanpcmcquen Nov 17 '16 at 09:27
  • @ryanpcmcquen Yes it works, when I'm using `Object.keys(data[0])` but without `[0]` I get more than the desired keys. – yuro Nov 17 '16 at 09:37
  • @yuro No. Idea solution is `Object.keys(data[0])`. Also if you are seeking improvements, post question on [CodeReviews](http://codereview.stackexchange.com/) – Rajesh Nov 17 '16 at 09:38
  • @yuro that is because you are feeding `Object.keys()` more than one object. You need to keep the `[0]` to only access the one correct Object, and hence the one set of keys you desire. – ryanpcmcquen Nov 17 '16 at 16:46
  • @ryanpcmcquen I'm aware. But I thought there is a smarter solution than that. – yuro Nov 17 '16 at 19:54
  • @yuro there is not, will you accept my answer? – ryanpcmcquen Jan 19 '17 at 22:26

2 Answers2

3

Use Object.keys():

var data = [{
  "NAME": "John Doe",
  "AGE": 25,
  "CITY": "New York",
  "COUNTRY": "USA"
}, {
  "NAME": "John Doe",
  "AGE": 25,
  "CITY": "New York",
  "COUNTRY": "USA"
}, {
  "NAME": "John Doe",
  "AGE": 25,
  "CITY": "New York",
  "COUNTRY": "USA"
}, {
  "NAME": "John Doe",
  "AGE": 25,
  "CITY": "New York",
  "COUNTRY": "USA"
}, {
  "NAME": "John Doe",
  "AGE": 25,
  "CITY": "New York",
  "COUNTRY": "USA"
}]
var arrItems = Object.keys(data[0]);
console.log(arrItems);

https://repl.it/E0lI

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

ryanpcmcquen
  • 6,285
  • 3
  • 24
  • 37
  • This has been answered in post I have shared. You should not answer questions that are very basic. Instead find a similar post and mark it as duplicate – Rajesh Nov 17 '16 at 09:41
  • @Rajesh the accepted answer on your link uses `for...in`, which is no longer the preferred way of accessing Object keys. – ryanpcmcquen Nov 17 '16 at 16:44
  • even that is covered in that post. I guess that has highest votes. – Rajesh Nov 17 '16 at 16:46
  • @Rajesh, sure but it may be difficult for a beginner to know the difference. – ryanpcmcquen Nov 17 '16 at 16:48
  • mate i don't want a debate but it's always better to mark post duplicate. That way user will get exposure to different approach and get to know lots of caveat through comments. There is nothing wrong but please use this privilege – Rajesh Nov 17 '16 at 16:58
0

You can use

var allKeys = [];
for(var key in myObject) allKeys.push(k);

console.log("Keys " + allKeys.length + " keys: " + allKeys);

Or if you are using Lodash/underscore they it makes your life even easier.

Ankit Tanna
  • 1,779
  • 8
  • 32
  • 59
  • I'll suggest you to use @ryanpcmcquen 's answer too but it looks shady when we do data[0] maybe use an object model instead – Ankit Tanna Nov 17 '16 at 09:58