I have an array of objects and I want to change the keys in 'data' array to match the label in headers array.
const headers = [{
label: 'First Name',
field: 'firstName'
}, {
label: 'Last Name',
field: 'lastName'
}]
const data = [{
firstName: 'John',
lastName: 'Doe'
}, {
firstName: 'ABC',
lastName: 'DEF'
}]
const headers = [{
label: 'First Name',
field: 'firstName'
}, {
label: 'Last Name',
field: 'lastName'
}]
const data = [{
firstName: 'John',
lastName: 'Doe'
}, {
firstName: 'ABC',
lastName: 'DEF'
}]
const mapHeaders = headers.reduce((a, c) => {
return {
...a,
[c.field]: c.label
}
}, {})
const result = data.map(item => {
return {
...item,
[mapHeaders[item]]: item
}
})
console.log(result)
Please advice.
Expected Result:
[{
'First Name': 'John',
'Last Name': 'Doe'
}, {
'First Name': 'ABC',
'Last Name': 'DEF'
}]