Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!