Javascript:获取JSON数组中的所有现有密钥

时间:2022-06-14 01:12:27

I have a JSON array like below:

我有一个像下面这样的JSON数组:

var jsonArray = [{"k1":"v1"},{"k2":"v2"},{"k3":"v3"},{"k4":"v4"},{"k5":"v5"}]

I don't know which keys does exists in this array. I want to get all the existing key from the array.

我不知道这个数组中存在哪些键。我想从数组中获取所有现有密钥。

It should be possible something like this:

它可能是这样的:

for(i=0;i<jsonArray.lenght;i++){
  // something like-  key = jsonArray[i].key
  // alert(key);
}

Please tell me the method or way to get all keys existing in Json array.

请告诉我获取Json数组中所有键的方法或方法。

Regards

问候

5 个解决方案

#1


9  

Try this:

尝试这个:

var L = jsonArray.length;
for (var i = 0; i < L; i++) {
    var obj = jsonArray[i];
    for (var j in obj) {
        alert(j);
    }
}

I've also made some modifications of your current code (like length caching).

我还对你当前的代码做了一些修改(比如长度缓存)。

#2


18  

Why don't you use a

你为什么不用

var jsonObject = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4","k5":"v5"}

instead of your

而不是你的

var jsonArray = [{"k1":"v1"},{"k2":"v2"},{"k3":"v3"},{"k4":"v4"},{"k5":"v5"}]

? Then the solution would be so simple: Object.keys(jsonObject).

?然后解决方案就这么简单:Object.keys(jsonObject)。

#3


5  

Loop through the object properties, and select the first "real" one (which given your data schema should be the only real one).

循环遍历对象属性,并选择第一个“真实”属性(给定您的数据模式应该是唯一真实的一个)。

var jsonArray = [{"k1":"v1"},{"k2":"v2"},{"k3":"v3"},{"k4":"v4"},{"k5":"v5"}]

for (var i = 0; i < jsonArray.length; i++) {
    for (var prop in jsonArray[i]) {
        if (jsonArray[i].hasOwnProperty(prop)) {
            var key = prop;
            break;
        }
    }
    alert(key);
}

See How to loop through items in a js object? for an explanation of why it's important to use hasOwnProperty here.

请参见如何遍历js对象中的项目?为了解释为什么在这里使用hasOwnProperty很重要。

#4


3  

Try this:

尝试这个:

jsonArray.reduce(function(keys, element){ 
    for (key in element) {
       keys.push(key);
    } 
    return keys; 
},[]);

This should also work for multiple keys in the array objects.

这也适用于数组对象中的多个键。

If you're supporting old browsers that don't have reduce and map, then consider using a shim.

如果您支持没有reduce和map的旧浏览器,请考虑使用垫片。

#5


1  

var id = { "object": "page", "entry": [{ "id": "1588811284674233", "time": 1511177084837, "messaging": [{ "sender": { "id": "1393377930761248" }, "recipient": { "id": "1588811284674233" }, "timestamp": 1511177084553, "message": { "mid": "mid.$cAAX_9pLcfu1mCnGmiVf2Sxd2erI2", "seq": 1882, "text": "a" } }] }] };

    function getKey(obj, data) {
//@author dvdieukhtn@gmail.com
      var data = data || [];
      if (obj) {
        var keys = Object.keys(obj);
        for (var pos in keys) {
          console.log();
          data.push(keys[pos]);
          if ((obj[keys[pos]].constructor === Array)) {
            for (var i = 0; i < obj[keys[pos]].length; i++) {
              getKey(obj[keys[pos]][i], data);
            }
          }
          else if (obj[keys[pos]].constructor === Object) {
            getKey(obj[keys[pos]], data);
          }
        }
        return data;
      }
    }
    console.log(getKey(id));

#1


9  

Try this:

尝试这个:

var L = jsonArray.length;
for (var i = 0; i < L; i++) {
    var obj = jsonArray[i];
    for (var j in obj) {
        alert(j);
    }
}

I've also made some modifications of your current code (like length caching).

我还对你当前的代码做了一些修改(比如长度缓存)。

#2


18  

Why don't you use a

你为什么不用

var jsonObject = {"k1":"v1","k2":"v2","k3":"v3","k4":"v4","k5":"v5"}

instead of your

而不是你的

var jsonArray = [{"k1":"v1"},{"k2":"v2"},{"k3":"v3"},{"k4":"v4"},{"k5":"v5"}]

? Then the solution would be so simple: Object.keys(jsonObject).

?然后解决方案就这么简单:Object.keys(jsonObject)。

#3


5  

Loop through the object properties, and select the first "real" one (which given your data schema should be the only real one).

循环遍历对象属性,并选择第一个“真实”属性(给定您的数据模式应该是唯一真实的一个)。

var jsonArray = [{"k1":"v1"},{"k2":"v2"},{"k3":"v3"},{"k4":"v4"},{"k5":"v5"}]

for (var i = 0; i < jsonArray.length; i++) {
    for (var prop in jsonArray[i]) {
        if (jsonArray[i].hasOwnProperty(prop)) {
            var key = prop;
            break;
        }
    }
    alert(key);
}

See How to loop through items in a js object? for an explanation of why it's important to use hasOwnProperty here.

请参见如何遍历js对象中的项目?为了解释为什么在这里使用hasOwnProperty很重要。

#4


3  

Try this:

尝试这个:

jsonArray.reduce(function(keys, element){ 
    for (key in element) {
       keys.push(key);
    } 
    return keys; 
},[]);

This should also work for multiple keys in the array objects.

这也适用于数组对象中的多个键。

If you're supporting old browsers that don't have reduce and map, then consider using a shim.

如果您支持没有reduce和map的旧浏览器,请考虑使用垫片。

#5


1  

var id = { "object": "page", "entry": [{ "id": "1588811284674233", "time": 1511177084837, "messaging": [{ "sender": { "id": "1393377930761248" }, "recipient": { "id": "1588811284674233" }, "timestamp": 1511177084553, "message": { "mid": "mid.$cAAX_9pLcfu1mCnGmiVf2Sxd2erI2", "seq": 1882, "text": "a" } }] }] };

    function getKey(obj, data) {
//@author dvdieukhtn@gmail.com
      var data = data || [];
      if (obj) {
        var keys = Object.keys(obj);
        for (var pos in keys) {
          console.log();
          data.push(keys[pos]);
          if ((obj[keys[pos]].constructor === Array)) {
            for (var i = 0; i < obj[keys[pos]].length; i++) {
              getKey(obj[keys[pos]][i], data);
            }
          }
          else if (obj[keys[pos]].constructor === Object) {
            getKey(obj[keys[pos]], data);
          }
        }
        return data;
      }
    }
    console.log(getKey(id));