1
I have an API that returns an array, containing values of the type:
[
{
"nome": "JOSE"
},
{
"nome": "MARIA"
},
{
"nome": "SERGIO"
}
]
I need to return so I can display the information type: Name : Name :Maria Name :Sergio
How do I do it?
I’ve cracked my skull here, but I can’t find any way... Thank you!
Follow the code the way I’m trying to do, someone
const rp = require('request-promise');
function main(params) {
// if (!params.name)
// {
// return { message: 'Nome não encontrado.' };
// }
return rp({
method: 'GET',
uri: `URL AQUI`,
json: true,
})
.then(body => {
for (var i = 0; i < 10; i++) {//coloquei até 10 só pra testar
var result = [];
console.log(i);
console.log(body.recordsets[0].length);
console.log(body.recordset[i]);
result.push(body.recordset[i]); // aqui eu tento colocar no array que criei em cima
}
return result[0] ;
})
.catch(err => {
return err;
});
}
EDIT @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
COMING BACK JUST TO SAY THANK YOU:
Bom galera, muito obrigado pela ajudar, o code ficou assim:
<!-- language: lang-js -->
const rp = require('request-promise');
function main(params) {
// if (!params.name)
// {
// return { message: 'Nome não encontrado.' };
// }
return rp({
method: 'GET',
uri: `.....`,
json: true,
})
.then(body => {
var result = {'name' :[]};
body.forEach((item) => {
Object.keys(item).forEach((propriedade) => {
result.name.push(item[propriedade])
console.log(propriedade, ' -> ', item[propriedade])
})
})
return result;
})
.catch(err => {
return err;
});
}
<!-- end snippet -->
Fantastic is this but my request only accepts that Return is a JSON obj and I have no idea how to do I thought of some output like: { "name": "JOSE","MARIA,"SERGIO" } this is possible?
– Emerson Ferreira
@Emersonferreiravc need to return all the names together? this return of yours is an invalid json, can confirm how you need it?
– h3nr1ke
Exactly, @h3nr1ke I need all the names together, I’m trying everything but I can’t, the structure is something like { "name": "JOSE","MARIA","SERGIO", ...} until there are no more items to add
– Emerson Ferreira
to be valid would have to be something like {name: "Jose, Maria, Sergio"} or even {name: ["Jose", "maria", "Sergio"]}, which of these you search?
– h3nr1ke
These are the processes related to your area: [{"name":"CON001 - CPF Query"},{"name":"AUX004 - Prevent Screen Lock"},{"name":"EMAIL002 - E-mail"},{"name":"EMAIL000 - POP3/SMTP"}] currently this is my answer and I wanted to make it more harmonious with only 1 field "name" and all the names inside it, you can understand me?
– Emerson Ferreira
Not much =(, the structure you show is really very common and honestly do not see why to treat this return, in my view is already good, when make a loop to display where you need, I believe that has no problem, maybe rework this return is "complicate" your work. But going on, in my view, this structure is what you look for, so it would only have a field name and a simple array in it {name: ["Jose", "maria", "Sergio"]}, you could display all together using object.name.Join(',') for example and display all together
– h3nr1ke