J'ai un fichier JSON et avec l'aide de @Supercool, j'ai pu obtenir les noms des clés pour chacun. Je voulais pouvoir obtenir le type de valeurs.

Structure JSON:

[
    {
        "id": 1536700,
        "title": "final_output",
        "error": "",
        "data": [
            {
                "metric": 4940616.0,
                "title": "d_revenue"
            },
            {
                "metric": 5132162.0,
                "title": "p_revenue"
            },
            {
                "metric": 4954576.0,
                "title": "s_revenue"
            },
            {
                "metric": 4882217.0,
                "title": "u_revenue"
            },
            {
                "metric": 4869609.0,
                "title": "t_revenue"
            },
            {
                "metric": 5075422.0,
                "title": "w_revenue"
            },
            {
                "metric": 4461996.0,
                "title": "v_revenue"
            }
        ]
    }
]

Structure suivante:

[
    {
        "run_id": 1536700,
        "code_title": "select_data",
        "error": "",
        "data": [
            {
                "user_name": "C_51",
                "num1": 51,
                "num2": 101,
                "num3": 151
            },
            {
                "user_name": "H_51",
                "num1": 51,
                "num2": 101,
                "num3": 151
            },
            {
                "user_name": "C_52",
                "num1": 52,
                "num2": 102,
                "num3": 152
            },
            {
                "user_name": "H_52",
                "num1": 52,
                "num2": 102,
                "num3": 152
            },
            {
                "user_name": "C_53",
                "num1": 53,
                "num2": 103,
                "num3": 153
            }
        ]
    }
]

Utilisez Object.keys (yourObject) pour obtenir les clés

 $.getJSON(api, function(elem) {
      let keys=elem.map( structure =>  Object.keys(structure.data[0]))         
   });

Exécutez l'extrait de code suivant pour vérifier s'il fonctionne

 let ele= [{"id": 1536700,"title": "final_output","error": "",
"data": [{"metric": 4940616.0,"title": "d_revenue"},{"metric": 5132162.0,"title": "p_revenue"},{"metric":4954576.0,"title": "s_revenue"},{"metric": 4882217.0,"title":"u_revenue"},{"metric": 4869609.0,"title":"t_revenue"},{"metric": 5075422.0,"title": "w_revenue"},{"metric": 4461996.0,"title": "v_revenue"}
]
},
{"run_id": 1536700,"code_title": "select_data","error": "",
  "data": [{"user_name": "C_51","num1": 51,"num2": 101,"num3": 151},{"user_name": "H_51","num1": 51,"num2": 101, "num3": 151},{"user_name": "C_52","num1": 52,"num2": 102,"num3": 152},{"user_name": "H_52","num1": 52,"num2": 102,"num3":152},{"user_name": "C_53","num1": 53,"num2": 103,"num3": 153}
]}]
 console.log(ele.map( structure =>  Object.keys(structure.data[0])))
 console.log(ele.map( structure =>  Object.values(structure.data[0])))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Je veux pouvoir obtenir le type de chacune des valeurs. Est-il possible pour moi d'obtenir cela pour les valeurs répertoriées ci-dessus?

[
  [
    number,
    string
  ],
  [
    string,
    number,
    number,
    number
  ]
]
2
noob 6 avril 2020 à 19:27

3 réponses

Javascript est un langage prototype, vous pouvez donc utiliser la propriété constructor.name sur n'importe quelle structure en js pour voir de quel type il s'agit.

Exemple simple:

const a = { t: 5, s: '', d: false, e:[], f:{}, d:5.53 };
for (let k in a) {
  console.log(k,a[k].constructor.name);
}
0
mplungjan 6 avril 2020 à 16:38

Effectuez une map() via Object.values (obj) et utilisez la valeur typeof pour renvoyer le type

let ele= [{"id": 1536700,"title": "final_output","error": "",
"data": [{"metric": 4940616.0,"title": "d_revenue"},{"metric": 5132162.0,"title": "p_revenue"},{"metric":4954576.0,"title": "s_revenue"},{"metric": 4882217.0,"title":"u_revenue"},{"metric": 4869609.0,"title":"t_revenue"},{"metric": 5075422.0,"title": "w_revenue"},{"metric": 4461996.0,"title": "v_revenue"}
]
},
{"run_id": 1536700,"code_title": "select_data","error": "",
  "data": [{"user_name": "C_51","num1": 51,"num2": 101,"num3": 151},{"user_name": "H_51","num1": 51,"num2": 101, "num3": 151},{"user_name": "C_52","num1": 52,"num2": 102,"num3": 152},{"user_name": "H_52","num1": 52,"num2": 102,"num3":152},{"user_name": "C_53","num1": 53,"num2": 103,"num3": 153}
]}]
console.log("Keys are ..");
 console.log(ele.map( structure =>  Object.keys(structure.data[0])))
console.log("and their correspondent value types are ..."); 
 console.log(ele.map( structure =>  Object.values(structure.data[0]).map(value => typeof value)))
.as-console-wrapper { max-height: 100% !important; top: 0; }
0
Supercool. 6 avril 2020 à 16:37
let ele = [{"id": 1536700,"title": "final_output","error": "", "data": [{"metric": 4940616.0,"title": "d_revenue"},{"metric": 5132162.0,"title": "p_revenue"},{"metric":4954576.0,"title": "s_revenue"},{"metric": 4882217.0,"title":"u_revenue"},{"metric": 4869609.0,"title":"t_revenue"},{"metric": 5075422.0,"title": "w_revenue"},{"metric": 4461996.0,"title": "v_revenue"}]}, {"run_id": 1536700,"code_title": "select_data","error": "", "data": [{"user_name": "C_51","num1": 51,"num2": 101,"num3": 151},{"user_name": "H_51","num1": 51,"num2": 101, "num3": 151},{"user_name": "C_52","num1": 52,"num2": 102,"num3": 152},{"user_name": "H_52","num1": 52,"num2": 102,"num3":152},{"user_name": "C_53","num1": 53,"num2": 103,"num3": 153}]}];

console.log(ele.map(structure => Object.values(structure.data[0]).map(v => typeof v)));
1
subarachnid 6 avril 2020 à 16:35