Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
Set is not taking in a string in JavaScript

I am getting a collection of a data, which is questions from mongoDB, and I putting that in a set of random question. The set is not taking the input, and it is returning empty.

 module.exports.java = async (req, resp) => {
// const choice = req.body;
let quest = [];
let option = [];
let sort =[];
try {
    let data = await Questions.find({ type: 'javascript'})
    //for creating an array of questions
    data.forEach((elements) => {
        //Stores all the string in an array
        quest.push(elements.description);
        option.push(elements.options);
    })
    const unique = new Set()
    for (let i =0; i<10;i++){
        let rand = Math.floor(Math.random()*20+0);
        let input = quest[rand]
        unique.add(input);
        // console.log(typeof quest[rand])
        console.log('Inside the loop, quest: ' + input)
        console.log('Inside the loop, unique: ' + unique)
    }
    resp.send(unique)
}catch(e) {
    console.log(e)
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's probably working, but a Set can get converted into an empty object when it's getting JSONed. That is, there's no "standard" representation for a Set in JSON, so you get a "{}". Convert it into an array first:

resp.send(Array.from(unique));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda