Last active
August 3, 2023 12:45
-
-
Save TheShinriel/a8cffc07bcd40bef36f4d528604e582c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 🔍 Fonctions d'Objets et JSON en JavaScript/TypeScript | |
// 🔍 Object.keys() : Récupérer les clés d'un objet | |
const person: { [key: string]: any } = { name: 'John', age: 30, city: 'New York' }; | |
const keys = Object.keys(person); | |
// 👉 Résultat : ['name', 'age', 'city'] (keys est un tableau) | |
// 🔍 Itération sur chaque clé et affichage de la clé et de la valeur | |
for (const key of keys) { | |
/* | |
👉 Résultat : | |
Clé : name, Valeur : John | |
Clé : age, Valeur : 30 | |
Clé : city, Valeur : New York | |
*/ | |
console.log(`Clé : ${key}, Valeur : ${person[key]}`); | |
} | |
// 🔍 Object.values() : Récupérer les valeurs d'un objet | |
const values = Object.values(person); | |
// 👉 Résultat : ['John', 30, 'New York'] (values est un tableau) | |
// 🔍 Object.entries() : Récupérer les paires clé-valeur d'un objet | |
const entries = Object.entries(person); | |
// 👉 Résultat : [['name', 'John'], ['age', 30], ['city', 'New York']] (entries est un tableau) | |
// 🔍 JSON.stringify() : Convertir un objet en chaîne JSON | |
const personJSON = JSON.stringify(person); | |
// 👉 Résultat : '{"name":"John","age":30,"city":"New York"}' (personJSON est une chaîne JSON) | |
// 🔍 JSON.parse() : Analyser une chaîne JSON en un objet JavaScript | |
const jsonString = '{"name":"John","age":30,"city":"New York"}'; | |
const parsedObject: { [key: string]: any } = JSON.parse(jsonString); | |
// 👉 Résultat : { name: 'John', age: 30, city: 'New York' } (parsedObject est un objet JavaScript) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment