Glenn Sampson
Glenn Sampson

Reputation: 1248

push array key to own array

I'm struggling to work out how I can push my array keys to an array of its own which I'm eventually going to use as column headings.

The below successfully logs my keys however the push is breaking

    let cols = []

    data.forEach(function (obj, index) {
      if (index === 0)
      {
        console.log(Object.keys(obj));
        this.cols.push(Object.keys(obj));
    }
    });

I don't actually have to loop the whole data object either I only need the first index but 1 step at a time!

My end result I expect would be cols = ["ValueDate", "AccountName", "Holding"]

Any thoughts please

Thnaks GWS

Data Extract:

[
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHAUD",
    "Holding": 318622.53
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHCAD",
    "Holding": 7195
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHEUR",
    "Holding": 5077.97
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHGBP",
    "Holding": 19625
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHJPY",
    "Holding": 16463
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHNZD",
    "Holding": 601.56
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHSGD",
    "Holding": 1000
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHUSD",
    "Holding": 1716906.25
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHAUD",
    "Holding": 318622.53
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHCAD",
    "Holding": 7195
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHEUR",
    "Holding": 5077.97
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHGBP",
    "Holding": 19625
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHJPY",
    "Holding": 16463
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHNZD",
    "Holding": 601.56
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHSGD",
    "Holding": 1000
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHUSD",
    "Holding": 1720781.25
  }
]

Upvotes: 0

Views: 47

Answers (3)

arcsin1
arcsin1

Reputation: 54

let cols = []

data.forEach(function (obj, index) {
  if (index === 0)
  {
    console.log(Object.keys(obj));
    cols.push(Object.keys(obj));
 }
});

console.log(cols)

Upvotes: 0

subramanian
subramanian

Reputation: 1305

use

for(var key in obj){
}

data.forEach(function (obj, index) {
  if(index == 0){
    for(var key in obj){
       console.log(key);
    }
  }  
});

to loop through the object and get the key values

Upvotes: 0

Anis Jonischkeit
Anis Jonischkeit

Reputation: 894

If you only need to look at the first index anyway, just use Object.keys(data[0]). see working example below.

data = [
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHAUD",
    "Holding": 318622.53
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHCAD",
    "Holding": 7195
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHEUR",
    "Holding": 5077.97
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHGBP",
    "Holding": 19625
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHJPY",
    "Holding": 16463
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHNZD",
    "Holding": 601.56
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHSGD",
    "Holding": 1000
  },
  {
    "ValueDate": "2017-04-26T14:16:00",
    "AccountName": "CASHUSD",
    "Holding": 1716906.25
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHAUD",
    "Holding": 318622.53
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHCAD",
    "Holding": 7195
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHEUR",
    "Holding": 5077.97
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHGBP",
    "Holding": 19625
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHJPY",
    "Holding": 16463
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHNZD",
    "Holding": 601.56
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHSGD",
    "Holding": 1000
  },
  {
    "ValueDate": "2017-04-27T14:16:00",
    "AccountName": "CASHUSD",
    "Holding": 1720781.25
  }
]

let cols = Object.keys(data[0])
console.log(cols)

Upvotes: 2

Related Questions