Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda