Created
September 26, 2023 13:16
-
-
Save matefs/500d75bb4b90795db5ff8a4b94cb0ee5 to your computer and use it in GitHub Desktop.
atribuir valores aleatorios para o json
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
function atribuirValoresAleatorios(json) { | |
// Converte a string JSON de volta para um objeto | |
const objeto = JSON.parse(json); | |
// Percorre todos os atributos do objeto | |
for (const chave in objeto) { | |
// Gera um valor aleatório para o atributo | |
const valorAleatorio = Math.floor(Math.random() * 100); | |
// Atribui o valor aleatório ao atributo | |
objeto[chave] = valorAleatorio; | |
} | |
// Retorna o objeto com os valores aleatórios | |
return objeto; | |
} | |
const objetoJSON = JSON.stringify({nome: "mateus", idade: 1}); | |
const objetoComValoresAleatorios = atribuirValoresAleatorios(objetoJSON); | |
console.log(objetoComValoresAleatorios); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Caso houver uma lista no JSON