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

188
Visualizações
How can i strip unwanted keys and containers from my array of objects

I have an array of objects that i need in a particular format. Currently it contains it's nested in such a way that it contains unwanted keys and container objects.

badArray = [
{
    "college": {
        "id": 1,
        "location": "Victoria",
        "rating": 10,
        "alumni": [
            {
                "alumni_id": 1,
                "alumni_position": 1
            },
            {
                "alumni_id": 2,
                "alumni_position": 3
            },
        ]
    }
},
{
    "college": {
        "id": 2,
        "location": "New York",
        "rating": 9,
        "alumni": [
            {
                "alumni_id": 5,
                "alumni_position": 7
            }
        ]
    }
}
]

What I'd like to get to is a less nested object with the following structure

goodArray = [
{
"id": 1,
"location": "Victoria",
"rating": 10,
"alumni": [
        {
         "alumni_id": 1,
         "alumni_position": 1
        },
        {
         "alumni_id": 2,
         "alumni_position": 3
        },             
    ]   
},
{   
"id": 2,
"location": "New York",
"rating": 9,
"alumni": [
        {
         "alumni_id": 5,
         "alumni_position": 7
        }
    ]
}
]

I can remove the unwanted key using

 Object.values(badArray[0])

But I'm really struggling to find a way to remove the unwanted outer container aswell.

Any help REALLY appreciated

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

0

You can just do an array map and return your desired array in this situation instead of trying to delete keys

badArray = [
{
    "college": {
        "id": 1,
        "location": "Victoria",
        "rating": 10,
        "alumni": [
            {
                "alumni_id": 1,
                "alumni_position": 1
            },
            {
                "alumni_id": 2,
                "alumni_position": 3
            },
        ]
    }
},
{
    "college": {
        "id": 2,
        "location": "New York",
        "rating": 9,
        "alumni": [
            {
                "alumni_id": 5,
                "alumni_position": 7
            }
        ]
    }
}
]

let goodArray = badArray.map(el =>  el.college)
console.log(goodArray)

about 4 years ago · Juan Pablo Isaza Relatório

0

Logic

  • Array.map through array.
  • Take Object.values of each Object in the array
  • Return the first value.

I have considered you have only one key in each object of the array. This solution will work for any valye for your key.

Working Fiddle

const badArray = [
  {
    college: {
      id: 1, location: "Victoria", rating: 10,
      alumni: [
        { alumni_id: 1, alumni_position: 1 },
        { alumni_id: 2, alumni_position: 3 },
      ],
    },
  },
  {
    college: {
      id: 2, location: "New York", rating: 9,
      alumni: [
        { alumni_id: 5, alumni_position: 7 },
      ],
    },
  },
];

const goodArray = badArray.map(item => Object.values(item)[0]);
console.log(goodArray);

If your key is constant as "college" you can perform this as.

const goodArray = badArray.map(item => item.college);
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