最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to access javascript keyvalue pairs in an array - Stack Overflow

programmeradmin0浏览0评论

I have the following javascript array:

var groupedDataSet1 = [{year: "0-1k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())},
            {year: "1-2k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())},
            {year: "2-3k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())},
            {year: "3-4k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())},
            {year: "4-5k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())}];

I'd like to programatically know how many key/value pairs I have in each entry.

Is there a way to know that groupedDataSet contains the keys year, value1, value2, and value3 while another javascript array might only contain year, value1 and value2?

Doing groupedDataSet[0].length doesn't work.

Thanks.

I have the following javascript array:

var groupedDataSet1 = [{year: "0-1k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())},
            {year: "1-2k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())},
            {year: "2-3k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())},
            {year: "3-4k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())},
            {year: "4-5k", value1: Math.floor(Math.random()), value2: Math.floor(Math.random()), value3: Math.floor(Math.random())}];

I'd like to programatically know how many key/value pairs I have in each entry.

Is there a way to know that groupedDataSet contains the keys year, value1, value2, and value3 while another javascript array might only contain year, value1 and value2?

Doing groupedDataSet[0].length doesn't work.

Thanks.

Share Improve this question asked Sep 3, 2013 at 17:06 Nitzan WilnaiNitzan Wilnai 9439 silver badges25 bronze badges 3
  • 1 See here: stackoverflow./questions/18912/how-to-find-keys-of-a-hash – just_dont Commented Sep 3, 2013 at 17:08
  • A object hash does not have the length property.. you will have to explicitly iterate over the keys to get the length – Sushanth -- Commented Sep 3, 2013 at 17:09
  • 1 Object.keys(groupedDataSet1[i]).length should do the trick. – raina77ow Commented Sep 3, 2013 at 17:09
Add a ment  | 

2 Answers 2

Reset to default 3
Object.keys(groupedDataSet[0]).length

should get you what you're looking for. It returns an array containing the instance keys in the object.

If the objects in the list may have different key set, then you have to check each object to collect all keys. You can do

var keys_memo = {};
groupedDataSet1.forEach(function (item) {
    for (var i in item) {
        keys_memo[i] = 1;
    }
});
var keys = Object.keys(keys_memo);

console.log(keys)
>>>["year", "value1", "value2", "value3"] 
发布评论

评论列表(0)

  1. 暂无评论