Compare the keys in the headerObject with the keys in dataObj and create a new object using the labels from headerObject.
const headerObject = [{
label: 'Code',
field_key: 'code'
},
{
label: 'Worked Accounts',
field_key: 'workedAccounts'
},
{
label: 'Contactable Accounts',
field_key: 'contactableAccounts'
},
{
label: 'Taken By',
field_key: 'takenBy'
},
];
const dataObj = [{
"code": "ABCD",
"takenBy": "",
"workedAccounts": 4,
"contactableAccounts": 3,
},
{
"takenBy": "Ram",
"workedAccounts": 2,
"contactableAccounts": 1,
},
{
"takenBy": "krish",
"workedAccounts": 2,
"contactableAccounts": 2,
},
{
"code": "XYZ",
"takenBy": "",
"workedAccounts": 9,
"contactableAccounts": 4,
},
{
"Taken By": "Jack",
"workedAccounts": 5,
"contactableAccounts": 0,
},
{
"Taken By": "krish",
"workedAccounts": 4,
"contactableAccounts": 4,
}
];
const result = [{
"Code": "ABCD",
"Taken By": "",
"Worked Accounts": 4,
"Contactable Accounts": 3,
},
{
"Taken By": "Ram",
"Worked Accounts": 2,
"Contactable Accounts": 1,
},
{
"Taken By": "krish",
"Worked Accounts": 2,
"Contactable Accounts": 2,
},
{
"Code": "XYZ",
"Taken By": "",
"Worked Accounts": 9,
"Contactable Accounts": 4,
},
{
"Taken By": "Jack",
"Worked Accounts": 5,
"Contactable Accounts": 0,
},
{
"Taken By": "krish",
"Worked Accounts": 4,
"Contactable Accounts": 4,
}
];
result = filter(headerObj, el =>
Object.keys(dataObj)?.filter(ele => ele === el.field_key && {[el.field.key]: ele[field.key));