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

175
Visualizações
Converting an object into an array

I have an object that contains other objects, as well as a few numbers i don't want.

I want to convert the object into an array of objects and remove the numbers.

an example:

INPUT

const object = {

  john: {
    instrument: 'violin',
    age: 26
  },

  bob: {
    instrument: 'guitar',
    age: 32
  },

  numberIDontWant: 2,

  flynn: {
    instrument: 'piano',
    age: 3
  },

  numberIDontWant2: 9
}

OUTPUT

  [
    {
      name: 'john',
      instrument: 'violin',
      age: 26
    },
    {
      name: 'bob',
      instrument: 'guitar',
      age: 32
    },
    {
      name: 'flynn',
      instrument: 'piano',
      age: 20
    }
  ]

Can anyone help me with that?

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

0

You could use Object.entries and .map:

console.log(Object.entries({
  john: {
    instrument: 'violin',
    age: 26
  },

  bob: {
    instrument: 'guitar',
    age: 32
  },

  numberIDontWant: 2,

  flynn: {
    instrument: 'piano',
    age: 3
  },

  numberIDontWant2: 9
}).map(([k,v]) => typeof v === 'object' ? ({...v, name: k}) : undefined).filter(i => i))

about 4 years ago · Juan Pablo Isaza Relatório

0

You'll most likely want to iterate over the Object.entries() so you have access to the key (the name) and the value. It isn't 100% clear what the criteria is for what values to include/exclude, but one way would be to only include the values that have a typeof equal to 'object'.

function objectToArray(obj) {
    let arr = [];
    for (let [name, value] of Object.entries(obj)) {
        if (typeof value === "object") {
            arr.push({
                ...value,
                name,
            });
        }
    }

    return arr;
}

console.log(
    objectToArray({
        john: {
            instrument: "violin",
            age: 26,
        },

        bob: {
            instrument: "guitar",
            age: 32,
        },

        numberIDontWant: 2,

        flynn: {
            instrument: "piano",
            age: 3,
        },

        numberIDontWant2: 9,
    })
);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use map and filter

const data = {
  john: {
    instrument: 'violin',
    age: 26
  },
  bob: {
    instrument: 'guitar',
    age: 32
  },
  numberIDontWant: 2,
  flynn: {
    instrument: 'piano',
    age: 3
  },
  numberIDontWant2: 9
}

const result = Object.entries(data).map(([k, v]) => {
    if (typeof v === "object") return { ...v, name: k }
}).filter(x => x);

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