[removed] Getting all existing keys in a JSON array

后端 未结 5 1251
独厮守ぢ
独厮守ぢ 2020-12-17 15:34

I have a JSON array like below:

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

I don\'t

5条回答
  •  别那么骄傲
    2020-12-17 15:56

    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.

提交回复
热议问题