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

167
Visualizações
ignore object name recebeuuser

I want it to ignore the element with the initial "recebeu" and only send what starts with "user", how can I do that?...

const array = [
  {
    member: "Joao",
    user: {

      user: "id1",
      user2: "id2",
      user3: "null",
      recebeuuser: "null"
    },
  },
  {
    member: "Pedro",
    user: {
      user: "id1",
      user2: "id2",
      user3: "null",
      recebeuuser: "name"
    },
  },
];

const filteredUsers = (user) => Object.values(user).filter(u =>
  u !== 'null' && typeof u === 'string'
)

const mentionUser = (user) => `<@${user}>`

const stringify = (m) => `${m.member} - ${filteredUsers(m.user).map(mentionUser).join(', ')}`
console.log(array.map(stringify).join('\n')) 
Joao - <@id1>, <@id2>
Pedro - <@id1>, <@id2>, <@name>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think part of the problem is that you're using a lot of shortcuts without fully understanding the shape of what you're passing around. I would recommend simplifying the code and avoiding shorthand notation.

Here is a refactored version of what you are doing using a simple for...of loop iterating over the Object.entries() of the user property of each passed object. If the key and value pass all the conditions a formatted string is pushed to the users array. Finally it returns the fully formatted string.

function stringifyMember(memberObject) {
  const { member, user } = memberObject;

  const users = [];
  for (const [k, u] of Object.entries(user)) {
    if (k.startsWith('user') && u !== 'null' && typeof u === 'string') {
      users.push(`<@${u}>`)
    }
  }

  return `${member} - ${users.join(', ')}`
}

const array = [{ member: "Joao", user: { user: "id1", user2: "id2", user3: "null", recebeuuser: "null" }, }, { member: "Pedro", user: { user: "id1", user2: "id2", user3: "null", recebeuuser: "name" }, },];

console.log(array.map(m => stringifyMember(m)).join('\n'))

also see:

  • String.prototype.startsWith()
  • Destructuring assignment
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