const cliente = {
nome: "Andre",
idade: 36,
cpf: "123.456.789.10",
email: "andre@gmail.com"
}
const chaves = ["nome", "idade", "cpf", "email"];
const clienteString = chaves.reduce((acum, curr) => acum += curr + ":" + cliente[curr] + ";")
console.log(clienteString);
the current output is: **nome**idade:36;cpf:123.456.789.10;email:andre@gmail.com;
the value of the key "nome:" is not being considered in the string
it should output this: nome:Andre;idade:36;cpf:123.456.789-10;email:andre@gmail.com;
what am i doing wrong?
This is a simpler version that does what you want:
const cliente = {
nome: "Andre",
idade: 36,
cpf: "123.456.789.10",
email: "andre@gmail.com",
};
console.log(
Object.entries(cliente)
.map(([key, value]) => `${key}:${value};`)
.join("")
);
Object.entries returns an array of ["key", "value"]. Then we map over that and turn it into an array of "key:value;". Then we join the array of "key:value;" into a string.