combine an array of object by id and and reduce its nested object Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Detecting an undefined object propertyWhat 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 to append something to an array?Sort array of objects by string property valueLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Providing direct feedback to a product salesperson

Why did Israel vote against lifting the American embargo on Cuba?

Assertions In A Mock Callout Test

Can gravitational waves pass through a black hole?

Marquee sign letters

Help Recreating a Table

Can I take recommendation from someone I met at a conference?

Trying to enter the Fox's den

A German immigrant ancestor has a "Registration Affidavit of Alien Enemy" on file. What does that mean exactly?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

Why isn't everyone flabbergasted about Bran's "gift"?

Why these surprising proportionalities of integrals involving odd zeta values?

Why do people think Winterfell crypts is the safest place for women, children & old people?

Why do C and C++ allow the expression (int) + 4*5?

Raising a bilingual kid. When should we introduce the majority language?

Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?

A journey... into the MIND

Determine the generator of an ideal of ring of integers

Why does my GNOME settings mention "Moto C Plus"?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Kepler's 3rd law: ratios don't fit data

Who's this lady in the war room?

How to make an animal which can only breed for a certain number of generations?

How can I introduce the names of fantasy creatures to the reader?



combine an array of object by id and and reduce its nested object



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Detecting an undefined object propertyWhat 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 to append something to an array?Sort array of objects by string property valueLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over 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;








1















I have an array of objects (old_array) that needs to be merged to become (new_array)



old_array = [
id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 ,
id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 ,
id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 ,
id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 ,
id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 ,
id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 ,
id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12
];

new_array = [
id: 'ffff55', name: 'f5', unique_cards: 2, request: device: 32, bus: 32, ship: 53 ,
id: 'vvvv44', name: 'v4', unique_cards: 1, request: device: 16, bus: 21, ship: 14 ,
id: 'cccc33', name: 'c3', unique_cards: 3, request: device: 25, bus: 23, ship: 26
];


  • merge the objects with same id and name to a single object

  • merge the nested request object (Sum of its values)

  • map card to the number of unique cards (by id)

I've been trying for 4 days straight but this array manipulation was hard



My best attempt was trying to group the array of objects by id but it become more complex with many redundant values



groupByArray(xs, key) 
return xs.reduce(function(rv, x)
let v = key instanceof Function ? key(x) : x[key];
let el = rv.find((r) => r && r.key === v);
if (el)
el.values.push(x);
else
rv.push( key: v, values: [ x ] );

return rv;
, []);


groupByArray(old_array , 'id')









share|improve this question



















  • 1





    show your best attempt in js

    – zerkms
    Mar 9 at 0:10











  • @zerkms i tried to group the arrays by id then mapping and reducing but it become more complex (more nested objects )

    – Amine Da.
    Mar 9 at 0:14











  • You have enumerated 3 different task. Try solve only the first one and publish your best attempt. That really is an EXTREMELY FREQUENT request here: at least make a research.

    – zerkms
    Mar 9 at 0:16












  • @zerkms i only ask if i'm very desperate . most of what i found about merging is either a simple array or using underscore.js loddah and other stuff. 14h straight today of searching

    – Amine Da.
    Mar 9 at 0:26











  • "is either a simple array" --- you have a simple array of simple objects. The problem here is that now you have an answer, yet you still are not able to solve such sort of tasks. You indeed can copy-paste it, but tomorrow you'll be stuck again when the conditions slightly change 🤷

    – zerkms
    Mar 9 at 0:27


















1















I have an array of objects (old_array) that needs to be merged to become (new_array)



old_array = [
id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 ,
id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 ,
id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 ,
id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 ,
id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 ,
id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 ,
id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12
];

new_array = [
id: 'ffff55', name: 'f5', unique_cards: 2, request: device: 32, bus: 32, ship: 53 ,
id: 'vvvv44', name: 'v4', unique_cards: 1, request: device: 16, bus: 21, ship: 14 ,
id: 'cccc33', name: 'c3', unique_cards: 3, request: device: 25, bus: 23, ship: 26
];


  • merge the objects with same id and name to a single object

  • merge the nested request object (Sum of its values)

  • map card to the number of unique cards (by id)

I've been trying for 4 days straight but this array manipulation was hard



My best attempt was trying to group the array of objects by id but it become more complex with many redundant values



groupByArray(xs, key) 
return xs.reduce(function(rv, x)
let v = key instanceof Function ? key(x) : x[key];
let el = rv.find((r) => r && r.key === v);
if (el)
el.values.push(x);
else
rv.push( key: v, values: [ x ] );

return rv;
, []);


groupByArray(old_array , 'id')









share|improve this question



















  • 1





    show your best attempt in js

    – zerkms
    Mar 9 at 0:10











  • @zerkms i tried to group the arrays by id then mapping and reducing but it become more complex (more nested objects )

    – Amine Da.
    Mar 9 at 0:14











  • You have enumerated 3 different task. Try solve only the first one and publish your best attempt. That really is an EXTREMELY FREQUENT request here: at least make a research.

    – zerkms
    Mar 9 at 0:16












  • @zerkms i only ask if i'm very desperate . most of what i found about merging is either a simple array or using underscore.js loddah and other stuff. 14h straight today of searching

    – Amine Da.
    Mar 9 at 0:26











  • "is either a simple array" --- you have a simple array of simple objects. The problem here is that now you have an answer, yet you still are not able to solve such sort of tasks. You indeed can copy-paste it, but tomorrow you'll be stuck again when the conditions slightly change 🤷

    – zerkms
    Mar 9 at 0:27














1












1








1








I have an array of objects (old_array) that needs to be merged to become (new_array)



old_array = [
id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 ,
id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 ,
id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 ,
id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 ,
id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 ,
id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 ,
id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12
];

new_array = [
id: 'ffff55', name: 'f5', unique_cards: 2, request: device: 32, bus: 32, ship: 53 ,
id: 'vvvv44', name: 'v4', unique_cards: 1, request: device: 16, bus: 21, ship: 14 ,
id: 'cccc33', name: 'c3', unique_cards: 3, request: device: 25, bus: 23, ship: 26
];


  • merge the objects with same id and name to a single object

  • merge the nested request object (Sum of its values)

  • map card to the number of unique cards (by id)

I've been trying for 4 days straight but this array manipulation was hard



My best attempt was trying to group the array of objects by id but it become more complex with many redundant values



groupByArray(xs, key) 
return xs.reduce(function(rv, x)
let v = key instanceof Function ? key(x) : x[key];
let el = rv.find((r) => r && r.key === v);
if (el)
el.values.push(x);
else
rv.push( key: v, values: [ x ] );

return rv;
, []);


groupByArray(old_array , 'id')









share|improve this question
















I have an array of objects (old_array) that needs to be merged to become (new_array)



old_array = [
id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 ,
id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 ,
id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 ,
id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 ,
id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 ,
id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 ,
id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12
];

new_array = [
id: 'ffff55', name: 'f5', unique_cards: 2, request: device: 32, bus: 32, ship: 53 ,
id: 'vvvv44', name: 'v4', unique_cards: 1, request: device: 16, bus: 21, ship: 14 ,
id: 'cccc33', name: 'c3', unique_cards: 3, request: device: 25, bus: 23, ship: 26
];


  • merge the objects with same id and name to a single object

  • merge the nested request object (Sum of its values)

  • map card to the number of unique cards (by id)

I've been trying for 4 days straight but this array manipulation was hard



My best attempt was trying to group the array of objects by id but it become more complex with many redundant values



groupByArray(xs, key) 
return xs.reduce(function(rv, x)
let v = key instanceof Function ? key(x) : x[key];
let el = rv.find((r) => r && r.key === v);
if (el)
el.values.push(x);
else
rv.push( key: v, values: [ x ] );

return rv;
, []);


groupByArray(old_array , 'id')






javascript typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 2:27









Dan

7,99253066




7,99253066










asked Mar 9 at 0:08









Amine Da.Amine Da.

7483917




7483917







  • 1





    show your best attempt in js

    – zerkms
    Mar 9 at 0:10











  • @zerkms i tried to group the arrays by id then mapping and reducing but it become more complex (more nested objects )

    – Amine Da.
    Mar 9 at 0:14











  • You have enumerated 3 different task. Try solve only the first one and publish your best attempt. That really is an EXTREMELY FREQUENT request here: at least make a research.

    – zerkms
    Mar 9 at 0:16












  • @zerkms i only ask if i'm very desperate . most of what i found about merging is either a simple array or using underscore.js loddah and other stuff. 14h straight today of searching

    – Amine Da.
    Mar 9 at 0:26











  • "is either a simple array" --- you have a simple array of simple objects. The problem here is that now you have an answer, yet you still are not able to solve such sort of tasks. You indeed can copy-paste it, but tomorrow you'll be stuck again when the conditions slightly change 🤷

    – zerkms
    Mar 9 at 0:27













  • 1





    show your best attempt in js

    – zerkms
    Mar 9 at 0:10











  • @zerkms i tried to group the arrays by id then mapping and reducing but it become more complex (more nested objects )

    – Amine Da.
    Mar 9 at 0:14











  • You have enumerated 3 different task. Try solve only the first one and publish your best attempt. That really is an EXTREMELY FREQUENT request here: at least make a research.

    – zerkms
    Mar 9 at 0:16












  • @zerkms i only ask if i'm very desperate . most of what i found about merging is either a simple array or using underscore.js loddah and other stuff. 14h straight today of searching

    – Amine Da.
    Mar 9 at 0:26











  • "is either a simple array" --- you have a simple array of simple objects. The problem here is that now you have an answer, yet you still are not able to solve such sort of tasks. You indeed can copy-paste it, but tomorrow you'll be stuck again when the conditions slightly change 🤷

    – zerkms
    Mar 9 at 0:27








1




1





show your best attempt in js

– zerkms
Mar 9 at 0:10





show your best attempt in js

– zerkms
Mar 9 at 0:10













@zerkms i tried to group the arrays by id then mapping and reducing but it become more complex (more nested objects )

– Amine Da.
Mar 9 at 0:14





@zerkms i tried to group the arrays by id then mapping and reducing but it become more complex (more nested objects )

– Amine Da.
Mar 9 at 0:14













You have enumerated 3 different task. Try solve only the first one and publish your best attempt. That really is an EXTREMELY FREQUENT request here: at least make a research.

– zerkms
Mar 9 at 0:16






You have enumerated 3 different task. Try solve only the first one and publish your best attempt. That really is an EXTREMELY FREQUENT request here: at least make a research.

– zerkms
Mar 9 at 0:16














@zerkms i only ask if i'm very desperate . most of what i found about merging is either a simple array or using underscore.js loddah and other stuff. 14h straight today of searching

– Amine Da.
Mar 9 at 0:26





@zerkms i only ask if i'm very desperate . most of what i found about merging is either a simple array or using underscore.js loddah and other stuff. 14h straight today of searching

– Amine Da.
Mar 9 at 0:26













"is either a simple array" --- you have a simple array of simple objects. The problem here is that now you have an answer, yet you still are not able to solve such sort of tasks. You indeed can copy-paste it, but tomorrow you'll be stuck again when the conditions slightly change 🤷

– zerkms
Mar 9 at 0:27






"is either a simple array" --- you have a simple array of simple objects. The problem here is that now you have an answer, yet you still are not able to solve such sort of tasks. You indeed can copy-paste it, but tomorrow you'll be stuck again when the conditions slightly change 🤷

– zerkms
Mar 9 at 0:27













1 Answer
1






active

oldest

votes


















1














You can use the function reduce to group and the function Object.values to extract the grouped objects.



Inside of the reduce's handler, you need to loop over the request's keys in order to sum up each value.






let array = [ id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 , id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 , id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 , id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 , id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12 ];
let result = Object.values(array.reduce((a, id, name, card, request) => 0) + request[k]);
return a;
, Object.create(null)));

result.forEach(o => delete o.card);

console.log(result);

.as-console-wrapper max-height: 100% !important; top: 0; 








share|improve this answer




















  • 1





    @AmineDa. you're right, so you need to check for the last validated card's value to increase the counter. See the updated answer, unfortunately, the last step should be loop the result and delete the card property.

    – Ele
    Mar 9 at 0:42







  • 1





    @AmineDa. basically, I'm asking for a previously captured object by id. So, If a[id] is different than undefined means that I got an object before so I need to work on it, otherwise, is a new id which I didn't get before, so I should create the desired object structure.

    – Ele
    Mar 9 at 1:02











  • @AmineDa. you're welcome!

    – Ele
    Mar 9 at 1:05











  • @.Ele one more thing. I don't get the point of a[id].card = card inside if

    – Amine Da.
    Mar 10 at 0:05







  • 1





    @AmineDa. this is like a breadcrumb, because I need to know if the previously checked card is different in order to increment the count.

    – Ele
    Mar 10 at 0:06












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%2f55072673%2fcombine-an-array-of-object-by-id-and-and-reduce-its-nested-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









1














You can use the function reduce to group and the function Object.values to extract the grouped objects.



Inside of the reduce's handler, you need to loop over the request's keys in order to sum up each value.






let array = [ id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 , id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 , id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 , id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 , id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12 ];
let result = Object.values(array.reduce((a, id, name, card, request) => 0) + request[k]);
return a;
, Object.create(null)));

result.forEach(o => delete o.card);

console.log(result);

.as-console-wrapper max-height: 100% !important; top: 0; 








share|improve this answer




















  • 1





    @AmineDa. you're right, so you need to check for the last validated card's value to increase the counter. See the updated answer, unfortunately, the last step should be loop the result and delete the card property.

    – Ele
    Mar 9 at 0:42







  • 1





    @AmineDa. basically, I'm asking for a previously captured object by id. So, If a[id] is different than undefined means that I got an object before so I need to work on it, otherwise, is a new id which I didn't get before, so I should create the desired object structure.

    – Ele
    Mar 9 at 1:02











  • @AmineDa. you're welcome!

    – Ele
    Mar 9 at 1:05











  • @.Ele one more thing. I don't get the point of a[id].card = card inside if

    – Amine Da.
    Mar 10 at 0:05







  • 1





    @AmineDa. this is like a breadcrumb, because I need to know if the previously checked card is different in order to increment the count.

    – Ele
    Mar 10 at 0:06
















1














You can use the function reduce to group and the function Object.values to extract the grouped objects.



Inside of the reduce's handler, you need to loop over the request's keys in order to sum up each value.






let array = [ id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 , id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 , id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 , id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 , id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12 ];
let result = Object.values(array.reduce((a, id, name, card, request) => 0) + request[k]);
return a;
, Object.create(null)));

result.forEach(o => delete o.card);

console.log(result);

.as-console-wrapper max-height: 100% !important; top: 0; 








share|improve this answer




















  • 1





    @AmineDa. you're right, so you need to check for the last validated card's value to increase the counter. See the updated answer, unfortunately, the last step should be loop the result and delete the card property.

    – Ele
    Mar 9 at 0:42







  • 1





    @AmineDa. basically, I'm asking for a previously captured object by id. So, If a[id] is different than undefined means that I got an object before so I need to work on it, otherwise, is a new id which I didn't get before, so I should create the desired object structure.

    – Ele
    Mar 9 at 1:02











  • @AmineDa. you're welcome!

    – Ele
    Mar 9 at 1:05











  • @.Ele one more thing. I don't get the point of a[id].card = card inside if

    – Amine Da.
    Mar 10 at 0:05







  • 1





    @AmineDa. this is like a breadcrumb, because I need to know if the previously checked card is different in order to increment the count.

    – Ele
    Mar 10 at 0:06














1












1








1







You can use the function reduce to group and the function Object.values to extract the grouped objects.



Inside of the reduce's handler, you need to loop over the request's keys in order to sum up each value.






let array = [ id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 , id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 , id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 , id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 , id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12 ];
let result = Object.values(array.reduce((a, id, name, card, request) => 0) + request[k]);
return a;
, Object.create(null)));

result.forEach(o => delete o.card);

console.log(result);

.as-console-wrapper max-height: 100% !important; top: 0; 








share|improve this answer















You can use the function reduce to group and the function Object.values to extract the grouped objects.



Inside of the reduce's handler, you need to loop over the request's keys in order to sum up each value.






let array = [ id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 , id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 , id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 , id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 , id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12 ];
let result = Object.values(array.reduce((a, id, name, card, request) => 0) + request[k]);
return a;
, Object.create(null)));

result.forEach(o => delete o.card);

console.log(result);

.as-console-wrapper max-height: 100% !important; top: 0; 








let array = [ id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 , id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 , id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 , id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 , id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12 ];
let result = Object.values(array.reduce((a, id, name, card, request) => 0) + request[k]);
return a;
, Object.create(null)));

result.forEach(o => delete o.card);

console.log(result);

.as-console-wrapper max-height: 100% !important; top: 0; 





let array = [ id: 'ffff55', name: 'f5', card: 'a', request: device: 0, bus: 1, ship: 21 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 3, bus: 10, ship: 2 , id: 'cccc33', name: 'c3', card: 'a', request: device: 0, bus: 1, ship: 2 , id: 'ffff55', name: 'f5', card: 'b', request: device: 32, bus: 31, ship: 32 , id: 'cccc33', name: 'c3', card: 'e', request: device: 21, bus: 21, ship: 22 , id: 'cccc33', name: 'c3', card: 'd', request: device: 4, bus: 1, ship: 2 , id: 'vvvv44', name: 'v4', card: 'c', request: device: 13, bus: 11, ship: 12 ];
let result = Object.values(array.reduce((a, id, name, card, request) => 0) + request[k]);
return a;
, Object.create(null)));

result.forEach(o => delete o.card);

console.log(result);

.as-console-wrapper max-height: 100% !important; top: 0; 






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 0:41

























answered Mar 9 at 0:20









EleEle

25.7k52252




25.7k52252







  • 1





    @AmineDa. you're right, so you need to check for the last validated card's value to increase the counter. See the updated answer, unfortunately, the last step should be loop the result and delete the card property.

    – Ele
    Mar 9 at 0:42







  • 1





    @AmineDa. basically, I'm asking for a previously captured object by id. So, If a[id] is different than undefined means that I got an object before so I need to work on it, otherwise, is a new id which I didn't get before, so I should create the desired object structure.

    – Ele
    Mar 9 at 1:02











  • @AmineDa. you're welcome!

    – Ele
    Mar 9 at 1:05











  • @.Ele one more thing. I don't get the point of a[id].card = card inside if

    – Amine Da.
    Mar 10 at 0:05







  • 1





    @AmineDa. this is like a breadcrumb, because I need to know if the previously checked card is different in order to increment the count.

    – Ele
    Mar 10 at 0:06













  • 1





    @AmineDa. you're right, so you need to check for the last validated card's value to increase the counter. See the updated answer, unfortunately, the last step should be loop the result and delete the card property.

    – Ele
    Mar 9 at 0:42







  • 1





    @AmineDa. basically, I'm asking for a previously captured object by id. So, If a[id] is different than undefined means that I got an object before so I need to work on it, otherwise, is a new id which I didn't get before, so I should create the desired object structure.

    – Ele
    Mar 9 at 1:02











  • @AmineDa. you're welcome!

    – Ele
    Mar 9 at 1:05











  • @.Ele one more thing. I don't get the point of a[id].card = card inside if

    – Amine Da.
    Mar 10 at 0:05







  • 1





    @AmineDa. this is like a breadcrumb, because I need to know if the previously checked card is different in order to increment the count.

    – Ele
    Mar 10 at 0:06








1




1





@AmineDa. you're right, so you need to check for the last validated card's value to increase the counter. See the updated answer, unfortunately, the last step should be loop the result and delete the card property.

– Ele
Mar 9 at 0:42






@AmineDa. you're right, so you need to check for the last validated card's value to increase the counter. See the updated answer, unfortunately, the last step should be loop the result and delete the card property.

– Ele
Mar 9 at 0:42





1




1





@AmineDa. basically, I'm asking for a previously captured object by id. So, If a[id] is different than undefined means that I got an object before so I need to work on it, otherwise, is a new id which I didn't get before, so I should create the desired object structure.

– Ele
Mar 9 at 1:02





@AmineDa. basically, I'm asking for a previously captured object by id. So, If a[id] is different than undefined means that I got an object before so I need to work on it, otherwise, is a new id which I didn't get before, so I should create the desired object structure.

– Ele
Mar 9 at 1:02













@AmineDa. you're welcome!

– Ele
Mar 9 at 1:05





@AmineDa. you're welcome!

– Ele
Mar 9 at 1:05













@.Ele one more thing. I don't get the point of a[id].card = card inside if

– Amine Da.
Mar 10 at 0:05






@.Ele one more thing. I don't get the point of a[id].card = card inside if

– Amine Da.
Mar 10 at 0:05





1




1





@AmineDa. this is like a breadcrumb, because I need to know if the previously checked card is different in order to increment the count.

– Ele
Mar 10 at 0:06






@AmineDa. this is like a breadcrumb, because I need to know if the previously checked card is different in order to increment the count.

– Ele
Mar 10 at 0:06




















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%2f55072673%2fcombine-an-array-of-object-by-id-and-and-reduce-its-nested-object%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

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved