1

I am trying to convert the data object to custom format

This is my data which i want to convert

[
  { Name: 15, GroupID: 1, Id: 1 },
  { Name: 16, GroupID: 1, Id: 1 },
  { Name: 17, GroupID: 2, Id: 2 },
  { Name: 18, GroupID: 2, Id: 2 },
  { Name: 15, GroupID: 3, Id: 3 },
  { Name: 16, GroupID: 3, Id: 3 },
  { Name: 17, GroupID: 4, Id: 4 },
  { Name: 18, GroupID: 4, Id: 4 }
];

This is what i want to acheive

  { GroupID: 1 },

  { Name: 15, Id: 1 },
  { Name: 16, Id: 1 },

  { GroupID: 2 },

  { Name: 17, Id: 2 },
  { Name: 18, Id: 2 },

  { GroupID: 3 },

  { Name: 15, Id: 3 },
  { Name: 16, Id: 3 },

  { GroupID: 4 },

  { Name: 17, Id: 4 },
  { Name: 18, Id: 4 }

This is what i have tried till now

var data = [
  { Name: 15, GroupID: 1, Id: 1 },
  { Name: 16, GroupID: 1, Id: 1 },
  { Name: 17, GroupID: 2, Id: 2 },
  { Name: 18, GroupID: 2, Id: 2 },
  { Name: 15, GroupID: 3, Id: 3 },
  { Name: 16, GroupID: 3, Id: 3 },
  { Name: 17, GroupID: 4, Id: 4 },
  { Name: 18, GroupID: 4, Id: 4 }
];

var previousGroupId;
var newObject = new Object();
for (index in data) {
  var groupId = data[index].GroupID;
  if (groupId != previousGroupId) {
    var newGroup = "GroupId" + groupId;
    newObject[newGroup] = new Array();
    for (index in data) {
      if (data[index].GroupID == groupId) {
        var customObject = {
          "GroupID": groupId,
          "Name": data[index].Name,
          "Id": data[index].Id
        };
        newObject[newGroup].push(customObject);
      }
    }
  }
  previousGroupId = groupId;
}

console.log(newObject);

even i tried to refer this Javascript group objects by property

any suggestions would be helpful.

4
  • please add all the code, you tried to the question. is the wanted result an array? please edit it for a valid dats structure. Commented Nov 6, 2019 at 7:46
  • i have added all the code via jsfiddle, i will edit the ds to be more clear Commented Nov 6, 2019 at 7:49
  • Is that an array? Commented Nov 6, 2019 at 7:49
  • How do I create a runnable stack snippet? Commented Nov 6, 2019 at 7:51

1 Answer 1

3

Assuming an array of objects as result set, you could take a hash table with arrays and take a flat array as result set.

var data = [{ Name: 15, GroupID: 1, Id: 1 }, { Name: 16, GroupID: 1, Id: 1 }, { Name: 17, GroupID: 2, Id: 2 }, { Name: 18, GroupID: 2, Id: 2 }, { Name: 15, GroupID: 3, Id: 3 }, { Name: 16, GroupID: 3, Id: 3 }, { Name: 17, GroupID: 4, Id: 4 }, { Name: 18, GroupID: 4, Id: 4 }],
    result = Object
        .values(data.reduce((r, { Name, GroupID, Id }) => {
            r[GroupID] = r[GroupID] || [{ GroupID }];
            r[GroupID].push({ Name, Id });
            return r;
        }, {}))
        .flat();

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

2 Comments

I was about to post almost the same solution .... with data.reduce((r, { GroupID, ...rest }) ... and ... r[GroupID].push({ ...rest });
@Bravo or just r[GroupID].push(rest)

Your Answer

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