I understand your question and I have the solution correct.
However, I don’t know if you want to do this for lack of choice or knowledge.
So I bring two solutions:
- The one who answers your question;
- The one who simplifies your life.
answering your question
According to the MDN web Docs:
The method JSON.stringify()
converts a Javascript object or value into a JSON string. Optionally replaces values if a replacement function is passed or optionally converts only specified properties into a replacement array.
The replacement array is nothing more than a list, where you put the name of each property you want to include in the conversion.
let selecionado
let humano;
humano = {
nome: "José",
idade: 33,
altura: "1.8m"
}
// output: {"nome":"José","idade":33}
selecionado = JSON.stringify(humano, ["nome", "idade"]);
console.log(selecionado);
// output: {"nome":"José"}
selecionado = JSON.stringify(humano, ["nome"]);
console.log(selecionado);
Already the replacement function, is a callback that takes two arguments: one with the object name and the other with the value.
This function can use the arguments to modify or filter the value.
When the function returns undefined
, value is filtered (removed from conversion).
When the function returns something other than undefined
, this value is used in the conversion.
let substituido
let humano;
humano = {
nome: "José",
idade: 44,
altura: "1.8m"
}
function substituicao(name, value) {
if (value.idade != 44) return undefined
return `${value.idade}`
}
// output: "José"
substituido = JSON.stringify(humano, (name, value) => value.nome);
console.log(substituido);
// output: 66
substituido = JSON.stringify(humano, substituicao);
console.log(substituido);
So the solution to your question would be this:
let projetos,
membro;
function substituicao(name, projetos) {
let nomes;
nomes = projetos.map(projeto => projeto.nomeProjetos);
nomes = nomes.join(", ");
return nomes;
}
membro = {
"NProjetos": [
{
"idProjeto": 2,
"nomeProjetos": "SGI"
}
],
"cargo": "Desenvolvedor",
"descricao": "Desenvolvedor PHP, JAVA",
"id": 2,
"imgMembros": [
{
"idImg": 2,
"url": "img/diego.png"
}
],
"nome": "Diego Rabelo",
"stats": 3
};
projetos = JSON.stringify(membro.NProjetos, substituicao);
console.log(projetos);
simplifying life
If you don’t need to string the object, before filtering the project names, you could map them this way:
let nomes;
membro = {
"NProjetos": [
{
"idProjeto": 2,
"nomeProjetos": "SGI"
}
],
"cargo": "Desenvolvedor",
"descricao": "Desenvolvedor PHP, JAVA",
"id": 2,
"imgMembros": [
{
"idImg": 2,
"url": "img/diego.png"
}
],
"nome": "Diego Rabelo",
"stats": 3
};
projetos = membro.NProjetos.map(projeto => projeto.nomeProjetos);
nomes = projetos.join(", ");
console.log(nomes);
And that double quote missing in
"NProjetos"
? Was it a mistake to write the question or is it wrong in the code?– Jéf Bueno
It is not a valid JSON, you can first test it here: http://jsonlint.com/
– BrTkCa
In fact, what is
membro
inJSON.stringify(membro.nomeProjetos)
? Apparently your doubt is simple, but you can’t understand what you want.– Jéf Bueno
Same typing error, Jbueno.
– Ítalo Cristo
I believe that if your interest is to take only the value, you can simply use:
var nomeProj = membro.NProjetos[0].nomeProjetos
, than making stringify.– BrTkCa
From what I understand this is also @Lucascosta, but my question is,
N
right designs? you want thenomeProjetos
of all?– Kenny Rafael
Based on JSON https://jsfiddle.net/lbclucascosta/5n8wdtmg/
– BrTkCa
Thank you, Lucas. That’s right Kenny.
– Ítalo Cristo