I have following input
const testObj = [
{id:1, name: 'name1',role:'Developer'},
{id:2, name: 'name2',role:'Lead'},
{id:3, name: 'name3',role:'QA'}
]
and i need to get the output in following format
{
Developer: {id:1,name:'name1',role:'Developer'},
Lead: {id:2,name:'name2',role:'Lead'},
QA: {id:3,name:'name3',role:'QA'}
}
and i tried to write the following code but i am not getting my desired output
const convert = (array,role) => {
var result = {}
for(let index = 0; index < array.length; index++){
result[array[index].id] = array[index].name;
}
return result;
}
and i am calling my method using following code
convert(testObj,'role');
How do i modify this method to get my desired output without using Reduce keyword and all