How to group and create new property in javascript object?Create GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I loop through or enumerate a JavaScript object?How do I include a JavaScript file in another JavaScript file?Convert form data to JavaScript object with jQueryHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Are there any normal animals in Pokemon universe?
Are polynomials with the same roots identical?
Scientist couple raises alien baby
Is it possible for a vehicle to be manufactured without a catalytic converter?
Why can I traceroute to this IP address, but not ping?
What are neighboring ports?
Separate SPI data
How to hide rifle during medieval town entrance inspection?
How can one's career as a reviewer be ended?
Which is the better way to call a method that is only available to one class that implements an interface but not the other one?
Bb13b9 confusion
Printing Pascal’s triangle for n number of rows in Python
Fermat's statement about the ancients: How serious was he?
I have a problematic assistant manager, but I can't fire him
Increase speed altering column on large table to NON NULL
Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?
New bike, tubeless tire will not inflate
Next date with distinct digits
Can a human be transformed into a Mind Flayer?
If I leave the US through an airport, do I have to return through the same airport?
Electricity free spaceship
Generate basis elements of the Steenrod algebra
Excel division by 0 error when trying to average results of formulas
Has there been a multiethnic Star Trek character?
How to group and create new property in javascript object?
Create GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I loop through or enumerate a JavaScript object?How do I include a JavaScript file in another JavaScript file?Convert form data to JavaScript object with jQueryHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an object in javascript:
"types": [
"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 1,
"DocumentName": "doc1"
,
"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 2,
"DocumentName": "doc2"
,
"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 4,
"DocumentName": "doc4"
,
"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"DocumentCode": 1,
"DocumentName": "doc1"
,
"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"DocumentCode": 2,
"DocumentName": "doc2"
]
How can I group by ProcessCode
this object to look like this?
"types": [
"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"Docs": [
"DocumentCode": 1,
"DocumentName": "doc1"
,
"DocumentCode": 2,
"DocumentName": "doc2"
,
"DocumentCode": 4,
"DocumentName": "doc4"
]
,
"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"Docs": [
"DocumentCode": 1,
"DocumentName": "doc1"
,
"DocumentCode": 2,
"DocumentName": "doc2"
]
}
]
I haved try with groupBy
of lodash, but It put ProcessCode as key and not like the stucture I want.
"types": [
{
"b4919f5a-98cf-e711-80f3-1458d0431600":
I have an object in javascript:
"types": [
"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 1,
"DocumentName": "doc1"
,
"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 2,
"DocumentName": "doc2"
,
"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"DocumentCode": 4,
"DocumentName": "doc4"
,
"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"DocumentCode": 1,
"DocumentName": "doc1"
,
"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"DocumentCode": 2,
"DocumentName": "doc2"
]
How can I group by ProcessCode
this object to look like this?
"types": [
"ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600",
"ProcessName": "processX",
"Docs": [
"DocumentCode": 1,
"DocumentName": "doc1"
,
"DocumentCode": 2,
"DocumentName": "doc2"
,
"DocumentCode": 4,
"DocumentName": "doc4"
]
,
"ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600",
"ProcessName": "processY",
"Docs": [
"DocumentCode": 1,
"DocumentName": "doc1"
,
"DocumentCode": 2,
"DocumentName": "doc2"
]
]
I haved try with groupBy
of lodash, but It put ProcessCode as key and not like the stucture I want.
"types": [
{
"b4919f5a-98cf-e711-80f3-1458d0431600": ...
javascript
javascript
javascript
javascript
asked Mar 24 at 19:49
Jon SudJon Sud
799
799
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use the function reduce
for grouping and the function Object.values
to extract the grouped objects.
let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];
let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327886%2fhow-to-group-and-create-new-property-in-javascript-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the function reduce
for grouping and the function Object.values
to extract the grouped objects.
let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];
let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |
You can use the function reduce
for grouping and the function Object.values
to extract the grouped objects.
let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];
let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |
You can use the function reduce
for grouping and the function Object.values
to extract the grouped objects.
let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];
let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
You can use the function reduce
for grouping and the function Object.values
to extract the grouped objects.
let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];
let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];
let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
let types = [ "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 2, "DocumentName": "doc2" , "ProcessCode": "b4919f5a-98cf-e711-80f3-1458d0431600", "ProcessName": "processX", "DocumentCode": 4, "DocumentName": "doc4" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 1, "DocumentName": "doc1" , "ProcessCode": "09c27913-98cf-e711-80f3-1458d0431600", "ProcessName": "processY", "DocumentCode": 2, "DocumentName": "doc2" ];
let result = Object.values(types.reduce((a, ProcessCode, ProcessName, DocumentCode, DocumentName) => , Object.create(null)));
console.log(result);
.as-console-wrapper max-height: 100% !important; top: 0;
answered Mar 24 at 19:53
EleEle
26.3k52354
26.3k52354
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327886%2fhow-to-group-and-create-new-property-in-javascript-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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