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" }
]
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"
}
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);
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);