0

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

3
  • 2
    What happens if there are two Developers? Commented Jun 30, 2018 at 12:12
  • @JonasW.@bambam i mistakenly wrote my output with same role, now i updated my output. please check. Commented Jun 30, 2018 at 12:15
  • Possible duplicate of How to convert an array of objects to object with key value pairs Commented Jun 30, 2018 at 12:16

4 Answers 4

1

You're not far off (if I assume the roles in the output should be like the roles in the input), but the body of your for should be

result[array[index].role] = array[index];

You were using the id rather than the role, and grabbing the name instead of the entire object.

Also note that there's no need for your second parameter (role) in the code you've shown or to go from your original to your new structure:

const testObj = [{id:1, name: 'name1',role:'Developer'},
{id:2, name: 'name2',role:'Lead'},
{id:3, name: 'name3',role:'QA'}]

const convert = (array) => {
  var result = {}
  for (let index = 0; index < array.length; index++) {
    result[array[index].role] = array[index];
  }
  return result
}

console.log(convert(testObj));
.as-console-wrapper {
  max-height: 100% !important;
}

We can simplify that a bit with for-of, though:

const testObj = [{id:1, name: 'name1',role:'Developer'},
{id:2, name: 'name2',role:'Lead'},
{id:3, name: 'name3',role:'QA'}];

const convert = (array) => {
  const result = {};
  for (const entry of array) {
    result[entry.role] = entry;
  }
  return result;
}

console.log(convert(testObj));
.as-console-wrapper {
  max-height: 100% !important;
}


Note that as Jonas W. points out, your result structure only allows for a single person in each role. You might want to have the object store arrays instead:

const testObj = [{id:1, name: 'name1',role:'Developer'},
{id:2, name: 'name2',role:'Lead'},
{id:3, name: 'name3',role:'QA'},
{id:4, name: 'name4',role:'QA'}]

const convert = (array) => {
  var result = {}
  for (let index = 0; index < array.length; index++) {
    var roleArray = result[array[index].role];
    if (!roleArray) {
      roleArray = result[array[index].role] = [];
    }
    roleArray.push(array[index]);
  }
  return result
}

console.log(convert(testObj));
.as-console-wrapper {
  max-height: 100% !important;
}

or, again, with for-of:

const testObj = [{id:1, name: 'name1',role:'Developer'},
{id:2, name: 'name2',role:'Lead'},
{id:3, name: 'name3',role:'QA'},
{id:4, name: 'name4',role:'QA'}];

const convert = (array) => {
  const result = {}
  for (const entry of array) {
    let roleArray = result[entry.role];
    if (!roleArray) {
      roleArray = result[entry.role] = [];
    }
    roleArray.push(entry);
  }
  return result;
}

console.log(convert(testObj));
.as-console-wrapper {
  max-height: 100% !important;
}

Sign up to request clarification or add additional context in comments.

Comments

1

Your code groups the .names by .id, you want to group the objects itself by their role:

function group(array, key) {
  const result = {};
  for(const obj of array)
    result[ obj[key] ] = obj;
  return result;
}

group(testObj, "role")

Comments

1

const testObj = [{id:1, name: 'name1',role:'Developer'},
{id:2, name: 'name2',role:'Lead'},
{id:3, name: 'name3',role:'QA'}];

const convert = (array, role) => {
	let i = 0;
  const output = {};
	for (; i < array.length; i+= 1) {
  	output[array[i][role]] = array[i];
  }
  return output;
}

console.log(convert(testObj, 'role'));

Comments

0

You can do this with .reduce

const testObj = [{id:1, name: 'name1',role:'Developer'},{id:2, name: 'name2',role:'Lead'},{id:3, name: 'name3',role:'QA'}],
        result = testObj.reduce((acc, cur) => {
            (acc[cur.role] = acc[cur.role] || []);
            acc[cur.role]=cur;
            return acc;
        }, {});
        
    console.log(result);

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.