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

170
Visualizações
How to conditionally filter an array of objects that includes duplicates in Javascripts

I have an array of objects like following, some commands share the same text:

const Commands=[
{
  id: "id1",
  text: "read",
  message: "read",
},
{
  id: "id2",
  text: "read",
  message: "read a book",
},
{
  id: "id3",
  text: "sleep",
  message: "sleep",
},
{
  id: "id4",
  text: "help",
  message: "help",
},
{
  id: "id5",
  text: "help",
  message: "command help",
},
]

I want to filter this array out based on the user input message. Also, for the commands that have the same text, I only want the first one (or the default one). For example, if the user input ["read a book"], the result would be

[
{
  id: "id2",
  text: "read",
  message: "read a book",
},
{
  id: "id3",
  text: "sleep",
  message: "sleep",
},
{
  id: "id4",
  text: "help",
  message: "help",
},
]

if the user input ["read a message"], which does not exist in the array, the default one will be returned, the result would be

[
{
  id: "id1",
  text: "read",
  message: "read",
},
{
  id: "id3",
  text: "sleep",
  message: "sleep",
},
{
  id: "id4",
  text: "help",
  message: "help",
},
]

Here is my JavaScripts/TypeScripts,

const defaultReadText =
    Commands.find(
      (command) => command.message === userReadMessage
    )?.text ?? "read"
  const defaultReadMessage =
    Commands.find(
      (command) => command.message === userReadMessage
    )?.message ?? "read"

const defaultHelpText =
    Commands.find(
      (command) => command.message === userHelpMessage
    )?.text ?? "help"
  const defaultHelpMessage =
    Commands.find(
      (command) => command.message === userHelpMessage
    )?.message ?? "help"

 const enabledCommands =
    Commands.filter(
        (command) =>
          (command.text === defaultReadText &&
            command.message === defaultReadMessage) ||
          command.text != defaultReadText
      )?.filter(
        (command) =>
          (command.text === defaultHelpText &&
            command.message === defaultHelpMessage) ||
          command.text != defaultHelpText
      )

I wonder if there is better way to solve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could group by text and take the object for a matching message.

const
    filter = (array, message) => Object.values(array.reduce((r, o) => {
        if (!r[o.text] || o.message === message) r[o.text] = o;
        return r;
    }, {})),
    commands = [{ id: "id1", text: "read", message: "read" }, { id: "id2", text: "read", message: "read a book" }, { id: "id3", text: "sleep", message: "sleep" }, { id: "id4", text: "help", message: "help" }, { id: "id5", text: "help", message: "command help" }];

console.log(filter(commands));
console.log(filter(commands, "read a book"));
console.log(filter(commands, "read a message"));
.as-console-wrapper { max-height: 100% !important; top: 0; }

A chainable approach with reduce

const
    filter = message => (r, o) => {
        if (r[r.length - 1]?.text === o.text) {
            if (o.message === message) r[r.length - 1] = o;
        } else {
            r.push(o);
        }
        return r;
    },
    commands = [{ id: "id1", text: "read", message: "read" }, { id: "id2", text: "read", message: "read a book" }, { id: "id3", text: "sleep", message: "sleep" }, { id: "id4", text: "help", message: "help" }, { id: "id5", text: "help", message: "command help" }];

console.log(commands.reduce(filter(), []));
console.log(commands.reduce(filter("read a book"), []));
console.log(commands.reduce(filter("read a message"), []));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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