Consolidate matching elements and iterate and separate out elements non matching elements in a collection










0















I'm new to NodeJS and stuck on how to consolidate matching elements and iterate non matching elements in a collecting creating a new collection. I have thus far used lodash to group the data that is pasted(Grouped Input) below but cant figure out how to get Desired Output.



I'm open to using lodashunderscorejavascript or any other library compatible with Node.



Thanks for your help and suggestions.






Grouped Input:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM10',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0900' ,
mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM12',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '1015'
]

Desired Output:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation_1: 'ROOM10',
mod_AppointmentTime_1:'0900',
mod_AppointmentLocation_2: 'ROOM11',
mod_AppointmentTime_2:'0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation_1: 'ROOM11',
mod_AppointmentTime_1:'0930',
mod_AppointmentLocation_2: 'ROOM12',
mod_AppointmentTime_2:'1015'
]












share|improve this question






















  • Are you stuck with that output format? In general when you're tempted to use variable names with numbers like mod_AppointmentTime_1 & mod_AppointmentTime_2 you should be using an array.

    – Mark Meyer
    Nov 16 '18 at 4:09
















0















I'm new to NodeJS and stuck on how to consolidate matching elements and iterate non matching elements in a collecting creating a new collection. I have thus far used lodash to group the data that is pasted(Grouped Input) below but cant figure out how to get Desired Output.



I'm open to using lodashunderscorejavascript or any other library compatible with Node.



Thanks for your help and suggestions.






Grouped Input:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM10',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0900' ,
mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM12',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '1015'
]

Desired Output:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation_1: 'ROOM10',
mod_AppointmentTime_1:'0900',
mod_AppointmentLocation_2: 'ROOM11',
mod_AppointmentTime_2:'0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation_1: 'ROOM11',
mod_AppointmentTime_1:'0930',
mod_AppointmentLocation_2: 'ROOM12',
mod_AppointmentTime_2:'1015'
]












share|improve this question






















  • Are you stuck with that output format? In general when you're tempted to use variable names with numbers like mod_AppointmentTime_1 & mod_AppointmentTime_2 you should be using an array.

    – Mark Meyer
    Nov 16 '18 at 4:09














0












0








0








I'm new to NodeJS and stuck on how to consolidate matching elements and iterate non matching elements in a collecting creating a new collection. I have thus far used lodash to group the data that is pasted(Grouped Input) below but cant figure out how to get Desired Output.



I'm open to using lodashunderscorejavascript or any other library compatible with Node.



Thanks for your help and suggestions.






Grouped Input:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM10',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0900' ,
mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM12',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '1015'
]

Desired Output:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation_1: 'ROOM10',
mod_AppointmentTime_1:'0900',
mod_AppointmentLocation_2: 'ROOM11',
mod_AppointmentTime_2:'0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation_1: 'ROOM11',
mod_AppointmentTime_1:'0930',
mod_AppointmentLocation_2: 'ROOM12',
mod_AppointmentTime_2:'1015'
]












share|improve this question














I'm new to NodeJS and stuck on how to consolidate matching elements and iterate non matching elements in a collecting creating a new collection. I have thus far used lodash to group the data that is pasted(Grouped Input) below but cant figure out how to get Desired Output.



I'm open to using lodashunderscorejavascript or any other library compatible with Node.



Thanks for your help and suggestions.






Grouped Input:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM10',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0900' ,
mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM12',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '1015'
]

Desired Output:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation_1: 'ROOM10',
mod_AppointmentTime_1:'0900',
mod_AppointmentLocation_2: 'ROOM11',
mod_AppointmentTime_2:'0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation_1: 'ROOM11',
mod_AppointmentTime_1:'0930',
mod_AppointmentLocation_2: 'ROOM12',
mod_AppointmentTime_2:'1015'
]








Grouped Input:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM10',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0900' ,
mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM12',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '1015'
]

Desired Output:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation_1: 'ROOM10',
mod_AppointmentTime_1:'0900',
mod_AppointmentLocation_2: 'ROOM11',
mod_AppointmentTime_2:'0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation_1: 'ROOM11',
mod_AppointmentTime_1:'0930',
mod_AppointmentLocation_2: 'ROOM12',
mod_AppointmentTime_2:'1015'
]





Grouped Input:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM10',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0900' ,
mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM12',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '1015'
]

Desired Output:

[ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation_1: 'ROOM10',
mod_AppointmentTime_1:'0900',
mod_AppointmentLocation_2: 'ROOM11',
mod_AppointmentTime_2:'0930' ,
mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation_1: 'ROOM11',
mod_AppointmentTime_1:'0930',
mod_AppointmentLocation_2: 'ROOM12',
mod_AppointmentTime_2:'1015'
]






javascript node.js underscore.js lodash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 4:01









Andy P.Andy P.

144




144












  • Are you stuck with that output format? In general when you're tempted to use variable names with numbers like mod_AppointmentTime_1 & mod_AppointmentTime_2 you should be using an array.

    – Mark Meyer
    Nov 16 '18 at 4:09


















  • Are you stuck with that output format? In general when you're tempted to use variable names with numbers like mod_AppointmentTime_1 & mod_AppointmentTime_2 you should be using an array.

    – Mark Meyer
    Nov 16 '18 at 4:09

















Are you stuck with that output format? In general when you're tempted to use variable names with numbers like mod_AppointmentTime_1 & mod_AppointmentTime_2 you should be using an array.

– Mark Meyer
Nov 16 '18 at 4:09






Are you stuck with that output format? In general when you're tempted to use variable names with numbers like mod_AppointmentTime_1 & mod_AppointmentTime_2 you should be using an array.

– Mark Meyer
Nov 16 '18 at 4:09













2 Answers
2






active

oldest

votes


















0














An overall better approach would be to convert to array those date/time/locations. But given the current question could do something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _(data)
.groupBy('mod_Id')
.values()
.map(x => _.values(_.transform(x, (r,c,i) =>
r[c.mod_Id] = _.extend(r[c.mod_Id],
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation$i+1`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate$i+1`]: c.mod_AppointmentDate,
[`mod_AppointmentTime$i+1`]: c.mod_AppointmentTime,
), )))
.flatten()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





The idea is to first group by the mod_Id then take the values map through them and transform them to one object. Since you are back into object form you get the values and last you flatten the array.



Somewhat simpler approach is to go with ES6 with something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = Object.values(data.reduce((r, c, i) =>
let _x = r[c.mod_Id] ? r[c.mod_Id]._x : 0
_x++
r[c.mod_Id] = Object.assign(r[c.mod_Id] , )).map((_x, ...rest) => rest)
console.log(result)





The only caveat is that this approach is not pure since it keeps track of the current item number via mutating the object with an _x property, which at the end is stripped via the map



In case you want to consider going after the array idea things get much simpler then:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _.chain(data)
.reduce((r,c,i) =>
r[c.mod_Id] = r[c.mod_Id] ? r[c.mod_Id] :
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
schedule:

r[c.mod_Id].schedule.push(
mod_AppointmentLocation: c.mod_AppointmentLocation,
mod_AppointmentDate: c.mod_AppointmentDate,
mod_AppointmentTime: c.mod_AppointmentTime
)
return r
, )
.values()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








share|improve this answer

























  • thanks Akrion of detailed suggestions. I like the array idea better and decided to use that. When I cut and paste the code you provide if get "schedule: [ [Object], [Object] ]". I double checked everything: I'm using Node with lodash 4.17.11 [ mod_Id: '123456', mod_AccountNumber: '0001', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', schedule: [ [Object], [Object] ] , mod_Id: '654321', mod_AccountNumber: '0001', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', schedule: [ [Object], [Object] ] ]

    – Andy P.
    Nov 16 '18 at 13:56












  • It seems to work on my end ... here is the jsFiddle: jsfiddle.net/akrion/2ekbnwoq

    – Akrion
    Nov 16 '18 at 16:30











  • Thanks! Before I commented I checked it with JSBin and it works there as well. Interesting, same exact code in Node JS produces [object object]. I even downgraded lodash to 4.17.10.

    – Andy P.
    Nov 16 '18 at 16:50












  • Weird. I get working code in Repl as well: repl.it/@akrion/arrayNodeJSSample

    – Akrion
    Nov 16 '18 at 17:07






  • 1





    Thanks Akiron. Appreciate all your help. Your code worked well for me.

    – Andy P.
    Nov 16 '18 at 20:58


















1














const groupBy, values, merge = require('lodash');
// input...
const output = values(
groupBy(input, 'id')
).map(chunks => merge(...chunks));





share|improve this answer


















  • 1





    Code only answers are discouraged — try adding a little explanation. Also, this doesn't result in the desired format It discards matching keys like mod_AppointmentLocation.

    – Mark Meyer
    Nov 16 '18 at 4:13











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331250%2fconsolidate-matching-elements-and-iterate-and-separate-out-elements-non-matching%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














An overall better approach would be to convert to array those date/time/locations. But given the current question could do something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _(data)
.groupBy('mod_Id')
.values()
.map(x => _.values(_.transform(x, (r,c,i) =>
r[c.mod_Id] = _.extend(r[c.mod_Id],
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation$i+1`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate$i+1`]: c.mod_AppointmentDate,
[`mod_AppointmentTime$i+1`]: c.mod_AppointmentTime,
), )))
.flatten()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





The idea is to first group by the mod_Id then take the values map through them and transform them to one object. Since you are back into object form you get the values and last you flatten the array.



Somewhat simpler approach is to go with ES6 with something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = Object.values(data.reduce((r, c, i) =>
let _x = r[c.mod_Id] ? r[c.mod_Id]._x : 0
_x++
r[c.mod_Id] = Object.assign(r[c.mod_Id] , )).map((_x, ...rest) => rest)
console.log(result)





The only caveat is that this approach is not pure since it keeps track of the current item number via mutating the object with an _x property, which at the end is stripped via the map



In case you want to consider going after the array idea things get much simpler then:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _.chain(data)
.reduce((r,c,i) =>
r[c.mod_Id] = r[c.mod_Id] ? r[c.mod_Id] :
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
schedule:

r[c.mod_Id].schedule.push(
mod_AppointmentLocation: c.mod_AppointmentLocation,
mod_AppointmentDate: c.mod_AppointmentDate,
mod_AppointmentTime: c.mod_AppointmentTime
)
return r
, )
.values()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








share|improve this answer

























  • thanks Akrion of detailed suggestions. I like the array idea better and decided to use that. When I cut and paste the code you provide if get "schedule: [ [Object], [Object] ]". I double checked everything: I'm using Node with lodash 4.17.11 [ mod_Id: '123456', mod_AccountNumber: '0001', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', schedule: [ [Object], [Object] ] , mod_Id: '654321', mod_AccountNumber: '0001', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', schedule: [ [Object], [Object] ] ]

    – Andy P.
    Nov 16 '18 at 13:56












  • It seems to work on my end ... here is the jsFiddle: jsfiddle.net/akrion/2ekbnwoq

    – Akrion
    Nov 16 '18 at 16:30











  • Thanks! Before I commented I checked it with JSBin and it works there as well. Interesting, same exact code in Node JS produces [object object]. I even downgraded lodash to 4.17.10.

    – Andy P.
    Nov 16 '18 at 16:50












  • Weird. I get working code in Repl as well: repl.it/@akrion/arrayNodeJSSample

    – Akrion
    Nov 16 '18 at 17:07






  • 1





    Thanks Akiron. Appreciate all your help. Your code worked well for me.

    – Andy P.
    Nov 16 '18 at 20:58















0














An overall better approach would be to convert to array those date/time/locations. But given the current question could do something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _(data)
.groupBy('mod_Id')
.values()
.map(x => _.values(_.transform(x, (r,c,i) =>
r[c.mod_Id] = _.extend(r[c.mod_Id],
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation$i+1`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate$i+1`]: c.mod_AppointmentDate,
[`mod_AppointmentTime$i+1`]: c.mod_AppointmentTime,
), )))
.flatten()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





The idea is to first group by the mod_Id then take the values map through them and transform them to one object. Since you are back into object form you get the values and last you flatten the array.



Somewhat simpler approach is to go with ES6 with something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = Object.values(data.reduce((r, c, i) =>
let _x = r[c.mod_Id] ? r[c.mod_Id]._x : 0
_x++
r[c.mod_Id] = Object.assign(r[c.mod_Id] , )).map((_x, ...rest) => rest)
console.log(result)





The only caveat is that this approach is not pure since it keeps track of the current item number via mutating the object with an _x property, which at the end is stripped via the map



In case you want to consider going after the array idea things get much simpler then:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _.chain(data)
.reduce((r,c,i) =>
r[c.mod_Id] = r[c.mod_Id] ? r[c.mod_Id] :
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
schedule:

r[c.mod_Id].schedule.push(
mod_AppointmentLocation: c.mod_AppointmentLocation,
mod_AppointmentDate: c.mod_AppointmentDate,
mod_AppointmentTime: c.mod_AppointmentTime
)
return r
, )
.values()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








share|improve this answer

























  • thanks Akrion of detailed suggestions. I like the array idea better and decided to use that. When I cut and paste the code you provide if get "schedule: [ [Object], [Object] ]". I double checked everything: I'm using Node with lodash 4.17.11 [ mod_Id: '123456', mod_AccountNumber: '0001', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', schedule: [ [Object], [Object] ] , mod_Id: '654321', mod_AccountNumber: '0001', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', schedule: [ [Object], [Object] ] ]

    – Andy P.
    Nov 16 '18 at 13:56












  • It seems to work on my end ... here is the jsFiddle: jsfiddle.net/akrion/2ekbnwoq

    – Akrion
    Nov 16 '18 at 16:30











  • Thanks! Before I commented I checked it with JSBin and it works there as well. Interesting, same exact code in Node JS produces [object object]. I even downgraded lodash to 4.17.10.

    – Andy P.
    Nov 16 '18 at 16:50












  • Weird. I get working code in Repl as well: repl.it/@akrion/arrayNodeJSSample

    – Akrion
    Nov 16 '18 at 17:07






  • 1





    Thanks Akiron. Appreciate all your help. Your code worked well for me.

    – Andy P.
    Nov 16 '18 at 20:58













0












0








0







An overall better approach would be to convert to array those date/time/locations. But given the current question could do something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _(data)
.groupBy('mod_Id')
.values()
.map(x => _.values(_.transform(x, (r,c,i) =>
r[c.mod_Id] = _.extend(r[c.mod_Id],
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation$i+1`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate$i+1`]: c.mod_AppointmentDate,
[`mod_AppointmentTime$i+1`]: c.mod_AppointmentTime,
), )))
.flatten()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





The idea is to first group by the mod_Id then take the values map through them and transform them to one object. Since you are back into object form you get the values and last you flatten the array.



Somewhat simpler approach is to go with ES6 with something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = Object.values(data.reduce((r, c, i) =>
let _x = r[c.mod_Id] ? r[c.mod_Id]._x : 0
_x++
r[c.mod_Id] = Object.assign(r[c.mod_Id] , )).map((_x, ...rest) => rest)
console.log(result)





The only caveat is that this approach is not pure since it keeps track of the current item number via mutating the object with an _x property, which at the end is stripped via the map



In case you want to consider going after the array idea things get much simpler then:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _.chain(data)
.reduce((r,c,i) =>
r[c.mod_Id] = r[c.mod_Id] ? r[c.mod_Id] :
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
schedule:

r[c.mod_Id].schedule.push(
mod_AppointmentLocation: c.mod_AppointmentLocation,
mod_AppointmentDate: c.mod_AppointmentDate,
mod_AppointmentTime: c.mod_AppointmentTime
)
return r
, )
.values()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








share|improve this answer















An overall better approach would be to convert to array those date/time/locations. But given the current question could do something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _(data)
.groupBy('mod_Id')
.values()
.map(x => _.values(_.transform(x, (r,c,i) =>
r[c.mod_Id] = _.extend(r[c.mod_Id],
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation$i+1`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate$i+1`]: c.mod_AppointmentDate,
[`mod_AppointmentTime$i+1`]: c.mod_AppointmentTime,
), )))
.flatten()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





The idea is to first group by the mod_Id then take the values map through them and transform them to one object. Since you are back into object form you get the values and last you flatten the array.



Somewhat simpler approach is to go with ES6 with something like this:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = Object.values(data.reduce((r, c, i) =>
let _x = r[c.mod_Id] ? r[c.mod_Id]._x : 0
_x++
r[c.mod_Id] = Object.assign(r[c.mod_Id] , )).map((_x, ...rest) => rest)
console.log(result)





The only caveat is that this approach is not pure since it keeps track of the current item number via mutating the object with an _x property, which at the end is stripped via the map



In case you want to consider going after the array idea things get much simpler then:






const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _.chain(data)
.reduce((r,c,i) =>
r[c.mod_Id] = r[c.mod_Id] ? r[c.mod_Id] :
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
schedule:

r[c.mod_Id].schedule.push(
mod_AppointmentLocation: c.mod_AppointmentLocation,
mod_AppointmentDate: c.mod_AppointmentDate,
mod_AppointmentTime: c.mod_AppointmentTime
)
return r
, )
.values()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>








const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _(data)
.groupBy('mod_Id')
.values()
.map(x => _.values(_.transform(x, (r,c,i) =>
r[c.mod_Id] = _.extend(r[c.mod_Id],
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation$i+1`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate$i+1`]: c.mod_AppointmentDate,
[`mod_AppointmentTime$i+1`]: c.mod_AppointmentTime,
), )))
.flatten()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _(data)
.groupBy('mod_Id')
.values()
.map(x => _.values(_.transform(x, (r,c,i) =>
r[c.mod_Id] = _.extend(r[c.mod_Id],
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation$i+1`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate$i+1`]: c.mod_AppointmentDate,
[`mod_AppointmentTime$i+1`]: c.mod_AppointmentTime,
), )))
.flatten()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = Object.values(data.reduce((r, c, i) =>
let _x = r[c.mod_Id] ? r[c.mod_Id]._x : 0
_x++
r[c.mod_Id] = Object.assign(r[c.mod_Id] , )).map((_x, ...rest) => rest)
console.log(result)





const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = Object.values(data.reduce((r, c, i) =>
let _x = r[c.mod_Id] ? r[c.mod_Id]._x : 0
_x++
r[c.mod_Id] = Object.assign(r[c.mod_Id] , )).map((_x, ...rest) => rest)
console.log(result)





const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _.chain(data)
.reduce((r,c,i) =>
r[c.mod_Id] = r[c.mod_Id] ? r[c.mod_Id] :
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
schedule:

r[c.mod_Id].schedule.push(
mod_AppointmentLocation: c.mod_AppointmentLocation,
mod_AppointmentDate: c.mod_AppointmentDate,
mod_AppointmentTime: c.mod_AppointmentTime
)
return r
, )
.values()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





const data = [ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' , mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' , mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' ]

const result = _.chain(data)
.reduce((r,c,i) =>
r[c.mod_Id] = r[c.mod_Id] ? r[c.mod_Id] :
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
schedule:

r[c.mod_Id].schedule.push(
mod_AppointmentLocation: c.mod_AppointmentLocation,
mod_AppointmentDate: c.mod_AppointmentDate,
mod_AppointmentTime: c.mod_AppointmentTime
)
return r
, )
.values()
.value()

console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 6:08

























answered Nov 16 '18 at 4:24









AkrionAkrion

9,54011224




9,54011224












  • thanks Akrion of detailed suggestions. I like the array idea better and decided to use that. When I cut and paste the code you provide if get "schedule: [ [Object], [Object] ]". I double checked everything: I'm using Node with lodash 4.17.11 [ mod_Id: '123456', mod_AccountNumber: '0001', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', schedule: [ [Object], [Object] ] , mod_Id: '654321', mod_AccountNumber: '0001', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', schedule: [ [Object], [Object] ] ]

    – Andy P.
    Nov 16 '18 at 13:56












  • It seems to work on my end ... here is the jsFiddle: jsfiddle.net/akrion/2ekbnwoq

    – Akrion
    Nov 16 '18 at 16:30











  • Thanks! Before I commented I checked it with JSBin and it works there as well. Interesting, same exact code in Node JS produces [object object]. I even downgraded lodash to 4.17.10.

    – Andy P.
    Nov 16 '18 at 16:50












  • Weird. I get working code in Repl as well: repl.it/@akrion/arrayNodeJSSample

    – Akrion
    Nov 16 '18 at 17:07






  • 1





    Thanks Akiron. Appreciate all your help. Your code worked well for me.

    – Andy P.
    Nov 16 '18 at 20:58

















  • thanks Akrion of detailed suggestions. I like the array idea better and decided to use that. When I cut and paste the code you provide if get "schedule: [ [Object], [Object] ]". I double checked everything: I'm using Node with lodash 4.17.11 [ mod_Id: '123456', mod_AccountNumber: '0001', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', schedule: [ [Object], [Object] ] , mod_Id: '654321', mod_AccountNumber: '0001', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', schedule: [ [Object], [Object] ] ]

    – Andy P.
    Nov 16 '18 at 13:56












  • It seems to work on my end ... here is the jsFiddle: jsfiddle.net/akrion/2ekbnwoq

    – Akrion
    Nov 16 '18 at 16:30











  • Thanks! Before I commented I checked it with JSBin and it works there as well. Interesting, same exact code in Node JS produces [object object]. I even downgraded lodash to 4.17.10.

    – Andy P.
    Nov 16 '18 at 16:50












  • Weird. I get working code in Repl as well: repl.it/@akrion/arrayNodeJSSample

    – Akrion
    Nov 16 '18 at 17:07






  • 1





    Thanks Akiron. Appreciate all your help. Your code worked well for me.

    – Andy P.
    Nov 16 '18 at 20:58
















thanks Akrion of detailed suggestions. I like the array idea better and decided to use that. When I cut and paste the code you provide if get "schedule: [ [Object], [Object] ]". I double checked everything: I'm using Node with lodash 4.17.11 [ mod_Id: '123456', mod_AccountNumber: '0001', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', schedule: [ [Object], [Object] ] , mod_Id: '654321', mod_AccountNumber: '0001', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', schedule: [ [Object], [Object] ] ]

– Andy P.
Nov 16 '18 at 13:56






thanks Akrion of detailed suggestions. I like the array idea better and decided to use that. When I cut and paste the code you provide if get "schedule: [ [Object], [Object] ]". I double checked everything: I'm using Node with lodash 4.17.11 [ mod_Id: '123456', mod_AccountNumber: '0001', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', schedule: [ [Object], [Object] ] , mod_Id: '654321', mod_AccountNumber: '0001', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', schedule: [ [Object], [Object] ] ]

– Andy P.
Nov 16 '18 at 13:56














It seems to work on my end ... here is the jsFiddle: jsfiddle.net/akrion/2ekbnwoq

– Akrion
Nov 16 '18 at 16:30





It seems to work on my end ... here is the jsFiddle: jsfiddle.net/akrion/2ekbnwoq

– Akrion
Nov 16 '18 at 16:30













Thanks! Before I commented I checked it with JSBin and it works there as well. Interesting, same exact code in Node JS produces [object object]. I even downgraded lodash to 4.17.10.

– Andy P.
Nov 16 '18 at 16:50






Thanks! Before I commented I checked it with JSBin and it works there as well. Interesting, same exact code in Node JS produces [object object]. I even downgraded lodash to 4.17.10.

– Andy P.
Nov 16 '18 at 16:50














Weird. I get working code in Repl as well: repl.it/@akrion/arrayNodeJSSample

– Akrion
Nov 16 '18 at 17:07





Weird. I get working code in Repl as well: repl.it/@akrion/arrayNodeJSSample

– Akrion
Nov 16 '18 at 17:07




1




1





Thanks Akiron. Appreciate all your help. Your code worked well for me.

– Andy P.
Nov 16 '18 at 20:58





Thanks Akiron. Appreciate all your help. Your code worked well for me.

– Andy P.
Nov 16 '18 at 20:58













1














const groupBy, values, merge = require('lodash');
// input...
const output = values(
groupBy(input, 'id')
).map(chunks => merge(...chunks));





share|improve this answer


















  • 1





    Code only answers are discouraged — try adding a little explanation. Also, this doesn't result in the desired format It discards matching keys like mod_AppointmentLocation.

    – Mark Meyer
    Nov 16 '18 at 4:13
















1














const groupBy, values, merge = require('lodash');
// input...
const output = values(
groupBy(input, 'id')
).map(chunks => merge(...chunks));





share|improve this answer


















  • 1





    Code only answers are discouraged — try adding a little explanation. Also, this doesn't result in the desired format It discards matching keys like mod_AppointmentLocation.

    – Mark Meyer
    Nov 16 '18 at 4:13














1












1








1







const groupBy, values, merge = require('lodash');
// input...
const output = values(
groupBy(input, 'id')
).map(chunks => merge(...chunks));





share|improve this answer













const groupBy, values, merge = require('lodash');
// input...
const output = values(
groupBy(input, 'id')
).map(chunks => merge(...chunks));






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 4:08









Loid GabaLoid Gaba

192




192







  • 1





    Code only answers are discouraged — try adding a little explanation. Also, this doesn't result in the desired format It discards matching keys like mod_AppointmentLocation.

    – Mark Meyer
    Nov 16 '18 at 4:13













  • 1





    Code only answers are discouraged — try adding a little explanation. Also, this doesn't result in the desired format It discards matching keys like mod_AppointmentLocation.

    – Mark Meyer
    Nov 16 '18 at 4:13








1




1





Code only answers are discouraged — try adding a little explanation. Also, this doesn't result in the desired format It discards matching keys like mod_AppointmentLocation.

– Mark Meyer
Nov 16 '18 at 4:13






Code only answers are discouraged — try adding a little explanation. Also, this doesn't result in the desired format It discards matching keys like mod_AppointmentLocation.

– Mark Meyer
Nov 16 '18 at 4:13


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331250%2fconsolidate-matching-elements-and-iterate-and-separate-out-elements-non-matching%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin