I'm new to Angular, I'm trying to parse an array inside my array, I am not able to parse this array as using my code it comes up with an error Unexpected token u in JSON at position 0, I've searched all over the internet and there are many examples but non is working in my case, I don't understand what is wrong with my code, you assistance is much appreciated.
data
{
"employees":[
{
"id":"f790cb86-4d41-401c-2a45-08d745b83164",
"firstName_FL":"Employee Name",
"employeeJobStatuses":[
{
"id":"bd58a1a3-6dd7-4cd5-6c79-08d76cf25ba3",
"status":"STATUS_ACTIVE"
}
]
}
]
}
employees.component.ts
table: {
headerRows: 1,
widths: [ '*', '*', '*', '*', '*', '*' ],
alignment: 'center',
body: [[
{text: 'Name', style: 'header'},
{text: 'Position', style: 'header'},
{text: 'Department', style: 'header'},
{text: 'Hiring Date', style: 'header'},
{text: 'First Contracting Salary', style: 'header'},
{text: 'Status', style: 'header'},
]]
.concat(this.jsonObj.map((element, i) =>
[
element['firstName_FL'],
element['position']['name_FL'],
element['department']['name_FL'],
element['hiringDate'].slice(0, 10),
element['firstContractingSalary'],
element.employeeJobStatuses.map((x, j) => ({ ...x,
Status: x
}))
]
))
}
All is coming up with the result I need except this line JSON.parse(element['employeeJobStatuses']['status'])
Parse your input to a valid JS object, afterwards do with it whatever needs to be done.
// your input
const jsonString = JSON.stringify({
"data": {
"employee": [{
"id": "f790cb86-4d41-401c-2a45-08d745b83164",
"firstName_FL": "Employee Name",
"employeeJobStatuses": [{
"id": "bd58a1a3-6dd7-4cd5-6c79-08d76cf25ba3",
"status": "STATUS_ACTIVE"
}]
}]
}
})
// parse your input to js valid object
const jsonObj = JSON.parse(jsonString);
// do wathever you need to do
const newObj = jsonObj.data.employee.map((element, i) => ({
...element,
employeeJobStatuses: element.employeeJobStatuses.map((x, j) => ({ ...x,
status: x.status
}))
}))
console.log(newObj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With