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.

12
  • 1
    Possible duplicate of Getting JavaScript object key list Commented Nov 17, 2016 at 8: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. Commented Nov 17, 2016 at 8: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 Commented Nov 17, 2016 at 9: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. Commented Nov 17, 2016 at 9:05
  • Have you tried Array.prototype.keys()? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Nov 17, 2016 at 9:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

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 the accepted answer on your link uses for...in, which is no longer the preferred way of accessing Object keys.
even that is covered in that post. I guess that has highest votes.
@Rajesh, sure but it may be difficult for a beginner to know the difference.
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
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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.