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

207
Visualizações
Decompose arrays and create new arrays in JavaScript

I have an array that has a job array inside each job object.

I want to combine the name of each object and the jobTitle of the same object to create a new object and put it inside the newArray array.

I want to do this for all objects in the data.

Unfortunately, jobTitle is not found in the loop

For further guidance, I have listed the expected output: below.

Thank you for helping me.

const data = 
  [ { id: 5
    , name: "Emma"
    , job: [ { jobTitle: "Teacher",    workExperience: 8 } ] 
    } 
  , { id: 5
    , name: "Charlotte"
    , job: [ { jobTitle: "programmer", workExperience: 3 } ] 
    } 
  , { id: 5
    , name: "Amelia"
    , job: [ { jobTitle: "Doctor",     workExperience: 5 } ] 
  } ] 

const newArray = [];
  data.forEach(item => {
    newArray.push({
      name: item.name,
      jobTitle: item.job.forEach(item => item.jobTitle)
    })
  })
console.log(newArray);

 Expected output:
    [
         { name: 'Emma', jobTitle: "Teacher" }
         { name: 'Charlotte', jobTitle: "programmer" }
         { name: 'Amelia', jobTitle: "Doctor" }
    ]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

foreach does not return anything, you're looking for map():

jobTitle: item.job.map(item => item.jobTitle)

const data = [
    {
        id: 5,
        name: "Emma",
        job: [
            { jobTitle: "Teacher", workExperience: 8 }
        ]
    }, {
        id: 5,
        name: "Charlotte",
        job: [
            { jobTitle: "programmer", workExperience: 3 }
        ]
    }, {
        id: 5,
        name: "Amelia",
        job: [
            { jobTitle: "Doctor", workExperience: 5 }
        ]
    },
]

const newArray = [];
data.forEach(item => {
    newArray.push({
        name: item.name,
        jobTitle: item.job.map(item => item.jobTitle)
    })
})
console.log(newArray);

{
    "name": "Emma",
    "jobTitle": [
      "Teacher"
    ]
}

If you'd like a string (as shown in your example output), we can join() the array:

jobTitle: item.job.map(item => item.jobTitle).join(' ');

const data = [
        {
            id: 5,
            name: "Emma",
            job: [
                { jobTitle: "Teacher", workExperience: 8 }
            ]
        }, {
            id: 5,
            name: "Charlotte",
            job: [
                { jobTitle: "programmer", workExperience: 3 }
            ]
        }, {
            id: 5,
            name: "Amelia",
            job: [
                { jobTitle: "Doctor", workExperience: 5 }
            ]
        },
    ]

    const newArray = [];
    data.forEach(item => {
        newArray.push({
            name: item.name,
            jobTitle: item.job.map(item => item.jobTitle).join(' ')
        })
    })
    console.log(newArray);

{
    "name": "Emma",
    "jobTitle": "Teacher"
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If the input data syntax is definitely correct, you can just access it directly from item without another loop, assuming there is always one job.

 const data = [
        {
            id: 5,
            name: "Emma",
            job: [
                { jobTitle: "Teacher", workExperience: 8 }
            ]
        }, {
            id: 5,
            name: "Charlotte",
            job: [
                { jobTitle: "programmer", workExperience: 3 }
            ]
        }, {
            id: 5,
            name: "Amelia",
            job: [
                { jobTitle: "Doctor", workExperience: 5 }
            ]
        },
    ]

    const newArray = [];
    data.forEach(item => {
        newArray.push({
            name: item.name,
            jobTitle: item.job[0].jobTitle
        })
    })
    console.log(newArray);

  

about 4 years ago · Juan Pablo Isaza Relatório

0

You are almost there. forEach return value is undefined. That is the reason you have the issue. Since there is only one item in the jobTitle array, it is easily achievable using the index directly.

const data = 
  [ { id: 5
    , name: "Emma"
    , job: [ { jobTitle: "Teacher",    workExperience: 8 } ] 
    } 
  , { id: 5
    , name: "Charlotte"
    , job: [ { jobTitle: "programmer", workExperience: 3 } ] 
    } 
  , { id: 5
    , name: "Amelia"
    , job: [ { jobTitle: "Doctor",     workExperience: 5 } ] 
  } ] 

const newArray = [];
  data.forEach(item => {
    newArray.push({
      name: item.name,
      jobTitle: item.job[0].jobTitle
    })
  })
console.log(newArray);

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