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

186
Visualizações
How to filter an array based on a field value in each object and group the remaining fields under that object

I am trying to modify an array. I have the following json.

    [
        { state: "NY", key: "A", value: "Alarm" },
        { state: "CT", key: "A", value: "Alarm" },
        { state: "NH", key: "A", value: "Alarm" },
        { state: "CT", key: "B", value: "Audible and Etched" },
        { state: "NH", key: "B", value: "Audible and Etched" },
        { state: "NY", key: "AA", value: "Active Disabling and Anti-Theft" },
        { state: "FL", key: "B", value: "Audible and Etched" },
        { state: "FL", key: "A", value: "Alarm" },
        { state: "FL", key: "AA", value: "Active Disabling and Anti-Theft" }
    ]

Based on each unique state , I need to group the key , values into dropdown-array as shown below

    [{
        state: "NY",
        dropdown: [
            { key: "A", value: "Alarm" },
            { key: "AA", value: "Active Disabling and Anti-Theft" }
        ]
    },
    {
        state: "CT",
        dropdown: [
            { key: "A", value: "Alarm" },
            { key: "B", value: "Audible and Etched" }
        ]
    },
    {
        state: "NH",
        dropdown: [
            { key: "A", value: "Alarm" },
            { key: "B", value: "Audible and Etched" }
        ]
    },
    {
        state: "FL",
        dropdown: [
            { key: "A", value: "Alarm" },
            { key: "B", value: "Audible and Etched" },
            { key: "AA", value: "Active Disabling and Anti-Theft" }
        ]
    }]

Any Help would be Appreciated. Thanks.

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

0

You can use Array.reduce to get the modified array.

const inputArray = [
        { state: "NY", key: "A", value: "Alarm" },
        { state: "CT", key: "A", value: "Alarm" },
        { state: "NH", key: "A", value: "Alarm" },
        { state: "CT", key: "B", value: "Audible and Etched" },
        { state: "NH", key: "B", value: "Audible and Etched" },
        { state: "NY", key: "AA", value: "Active Disabling and Anti-Theft" },
        { state: "FL", key: "B", value: "Audible and Etched" },
        { state: "FL", key: "A", value: "Alarm" },
        { state: "FL", key: "AA", value: "Active Disabling and Anti-Theft" }
];

const result = inputArray.reduce((acc, item) => {
   const exist = acc.find(e => e.state == item.state);
   if (exist) {
      exist.dropdown.push({
          key: item.key,
          value: item.value
      })
   } else {
      acc.push({
         state: item.state,
         dropdown: [{
           key: item.key,
           value: item.value
         }]
      });
   }
   return acc;
}, []);

console.log(result);

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