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;
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
|
show 2 more comments
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
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
|
show 2 more comments
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
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
javascript typescript
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
|
show 2 more comments
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
|
show 2 more comments
1 Answer
1
active
oldest
votes
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;
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 thecard
property.
– Ele
Mar 9 at 0:42
1
@AmineDa. basically, I'm asking for a previously captured object byid
. So, Ifa[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
|
show 1 more 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%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
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;
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 thecard
property.
– Ele
Mar 9 at 0:42
1
@AmineDa. basically, I'm asking for a previously captured object byid
. So, Ifa[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
|
show 1 more comment
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;
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 thecard
property.
– Ele
Mar 9 at 0:42
1
@AmineDa. basically, I'm asking for a previously captured object byid
. So, Ifa[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
|
show 1 more comment
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;
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;
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 thecard
property.
– Ele
Mar 9 at 0:42
1
@AmineDa. basically, I'm asking for a previously captured object byid
. So, Ifa[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
|
show 1 more comment
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 thecard
property.
– Ele
Mar 9 at 0:42
1
@AmineDa. basically, I'm asking for a previously captured object byid
. So, Ifa[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
|
show 1 more 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%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
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
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