1

I'm doing some data cleaning in Javascript, and I was wondering if there is a way to combine objects based on a common ID.

Given the following:

{"subject":"Hadji Singh","predicate":"nameOfUser","id":"3f540200-58b9-40a5-91c2-faafec75216f"}
{"subject":"Race Roger Bannon","predicate":"nameOfUser","id":"41376a49-34ee-4ed8-b5f5-3f8c92b107f8"}
{"subject":"Jessie Bannon","predicate":"nameOfUser","id":"9575cf33-8992-4763-81bb-fc640ffa3545"}
{"subject":"Adventurer","predicate":"departmentOfUser","id":"3f540200-58b9-40a5-91c2-faafec75216f"}
{"subject":"Bodyguard","predicate":"departmentOfUser","id":"41376a49-34ee-4ed8-b5f5-3f8c92b107f8"}
{"subject":"Adventurer","predicate":"departmentOfUser","id":"9575cf33-8992-4763-81bb-fc640ffa3545"}

Is there a way to turn it into something like this?

[{id:3f540200-58b9-40a5-91c2-faafec75216f, name:Hadji Singh, department:Adventurer},
{id:9575cf33-8992-4763-81bb-fc640ffa3545, name:Jessie Bannon, department:Adventurer},
{id:41376a49-34ee-4ed8-b5f5-3f8c92b107f8, name:Race Bannon, department:Bodyguard}]

I'm thinking of two for loops (maybe) and a lot of if's, but perhaps there's some way I'm missing.

I'm using node.js, if that means anything. Perhaps there's a module that can help?

Thanks.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
zack_falcon
  • 4,186
  • 20
  • 62
  • 108
  • 6
    Please include attempted solutions, why they didn't work, and the expected results. That would really help us to figure out the issue with your code. Thanks! – palaѕн Oct 18 '17 at 14:15
  • Use the `id` as a `HashMap` key and do recursive [`Object.assign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)s. You'll have to set rules for precendence though. Please provide more details and maybe some attempts. – zero298 Oct 18 '17 at 14:18
  • Using Underscore - https://stackoverflow.com/questions/19480008/javascript-merging-objects-by-id – Jaybird Oct 18 '17 at 14:23

1 Answers1

1

Use reduce method of an array:

let a = [{"subject":"Hadji Singh","predicate":"nameOfUser","id":"3f540200-58b9-40a5-91c2-faafec75216f"},
{"subject":"Race Roger Bannon","predicate":"nameOfUser","id":"41376a49-34ee-4ed8-b5f5-3f8c92b107f8"},
{"subject":"Jessie Bannon","predicate":"nameOfUser","id":"9575cf33-8992-4763-81bb-fc640ffa3545"},
{"subject":"Adventurer","predicate":"departmentOfUser","id":"3f540200-58b9-40a5-91c2-faafec75216f"},
{"subject":"Bodyguard","predicate":"departmentOfUser","id":"41376a49-34ee-4ed8-b5f5-3f8c92b107f8"},
{"subject":"Adventurer","predicate":"departmentOfUser","id":"9575cf33-8992-4763-81bb-fc640ffa3545"}]

let b = a.reduce((acc, val) => {
  let id = val.id
  let idx = acc.findIndex((e) => e.id == id);
  let subj = { id: id }
  let predicate = val['predicate'].split('OfUser')[0]
  subj[predicate] = val['subject']
  if (-1 == idx) {
    acc.push(subj);
  } else {
    acc[idx] = Object.assign(acc[idx], subj);
  }
  return acc
}, [])

console.log(b);

//[ { id: '3f540200-58b9-40a5-91c2-faafec75216f',
//    name: 'Hadji Singh',
//    department: 'Adventurer' },
//  { id: '41376a49-34ee-4ed8-b5f5-3f8c92b107f8',
//    name: 'Race Roger Bannon',
//    department: 'Bodyguard' },
//  { id: '9575cf33-8992-4763-81bb-fc640ffa3545',
//    name: 'Jessie Bannon',
//    department: 'Adventurer' } ]
zinovyev
  • 2,084
  • 1
  • 22
  • 32