Javascript: Group similar items in array based on a value [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
javascript | Object grouping
5 answers
I have an array of objects like this..
[
{
_id: 123
},
{
_id: 123
},
{
_id: 321
}
]
I want to group similar items by _id and also count how many items of each unique _id there are...
[
{
_id: 123
qty: 2
},
{
_id: 321
qty: 1
}
]
Whats the best way to do this using javascript?
javascript arrays
marked as duplicate by slider, Akrion, Phil
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 23:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
javascript | Object grouping
5 answers
I have an array of objects like this..
[
{
_id: 123
},
{
_id: 123
},
{
_id: 321
}
]
I want to group similar items by _id and also count how many items of each unique _id there are...
[
{
_id: 123
qty: 2
},
{
_id: 321
qty: 1
}
]
Whats the best way to do this using javascript?
javascript arrays
marked as duplicate by slider, Akrion, Phil
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 23:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 19 at 23:32
1
I did try a lot of answers from SO but couldn't get them to work with my particular case.
– Serks
Nov 19 at 23:51
2
That's not the same as trying to write code yourself - when you post a question, please post the code you're trying to use as well
– CertainPerformance
Nov 19 at 23:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
javascript | Object grouping
5 answers
I have an array of objects like this..
[
{
_id: 123
},
{
_id: 123
},
{
_id: 321
}
]
I want to group similar items by _id and also count how many items of each unique _id there are...
[
{
_id: 123
qty: 2
},
{
_id: 321
qty: 1
}
]
Whats the best way to do this using javascript?
javascript arrays
This question already has an answer here:
javascript | Object grouping
5 answers
I have an array of objects like this..
[
{
_id: 123
},
{
_id: 123
},
{
_id: 321
}
]
I want to group similar items by _id and also count how many items of each unique _id there are...
[
{
_id: 123
qty: 2
},
{
_id: 321
qty: 1
}
]
Whats the best way to do this using javascript?
This question already has an answer here:
javascript | Object grouping
5 answers
javascript arrays
javascript arrays
asked Nov 19 at 23:32
Serks
8712
8712
marked as duplicate by slider, Akrion, Phil
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 23:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by slider, Akrion, Phil
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 23:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 19 at 23:32
1
I did try a lot of answers from SO but couldn't get them to work with my particular case.
– Serks
Nov 19 at 23:51
2
That's not the same as trying to write code yourself - when you post a question, please post the code you're trying to use as well
– CertainPerformance
Nov 19 at 23:54
add a comment |
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 19 at 23:32
1
I did try a lot of answers from SO but couldn't get them to work with my particular case.
– Serks
Nov 19 at 23:51
2
That's not the same as trying to write code yourself - when you post a question, please post the code you're trying to use as well
– CertainPerformance
Nov 19 at 23:54
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 19 at 23:32
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 19 at 23:32
1
1
I did try a lot of answers from SO but couldn't get them to work with my particular case.
– Serks
Nov 19 at 23:51
I did try a lot of answers from SO but couldn't get them to work with my particular case.
– Serks
Nov 19 at 23:51
2
2
That's not the same as trying to write code yourself - when you post a question, please post the code you're trying to use as well
– CertainPerformance
Nov 19 at 23:54
That's not the same as trying to write code yourself - when you post a question, please post the code you're trying to use as well
– CertainPerformance
Nov 19 at 23:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You can do this in O(n) time using Array.prototype.reduce()
and a Map
as the accumulator:
const array = [{'_id':123},{'_id':123},{'_id':321}]
const groups = [...array.reduce(
(map, { _id }) => map.set(_id, (map.get(_id) || 0) + 1),
new Map()
)].map(
([_id, qty]) => ({ _id, qty })
)
console.log(groups)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can do this in O(n) time using Array.prototype.reduce()
and a Map
as the accumulator:
const array = [{'_id':123},{'_id':123},{'_id':321}]
const groups = [...array.reduce(
(map, { _id }) => map.set(_id, (map.get(_id) || 0) + 1),
new Map()
)].map(
([_id, qty]) => ({ _id, qty })
)
console.log(groups)
add a comment |
up vote
1
down vote
accepted
You can do this in O(n) time using Array.prototype.reduce()
and a Map
as the accumulator:
const array = [{'_id':123},{'_id':123},{'_id':321}]
const groups = [...array.reduce(
(map, { _id }) => map.set(_id, (map.get(_id) || 0) + 1),
new Map()
)].map(
([_id, qty]) => ({ _id, qty })
)
console.log(groups)
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can do this in O(n) time using Array.prototype.reduce()
and a Map
as the accumulator:
const array = [{'_id':123},{'_id':123},{'_id':321}]
const groups = [...array.reduce(
(map, { _id }) => map.set(_id, (map.get(_id) || 0) + 1),
new Map()
)].map(
([_id, qty]) => ({ _id, qty })
)
console.log(groups)
You can do this in O(n) time using Array.prototype.reduce()
and a Map
as the accumulator:
const array = [{'_id':123},{'_id':123},{'_id':321}]
const groups = [...array.reduce(
(map, { _id }) => map.set(_id, (map.get(_id) || 0) + 1),
new Map()
)].map(
([_id, qty]) => ({ _id, qty })
)
console.log(groups)
const array = [{'_id':123},{'_id':123},{'_id':321}]
const groups = [...array.reduce(
(map, { _id }) => map.set(_id, (map.get(_id) || 0) + 1),
new Map()
)].map(
([_id, qty]) => ({ _id, qty })
)
console.log(groups)
const array = [{'_id':123},{'_id':123},{'_id':321}]
const groups = [...array.reduce(
(map, { _id }) => map.set(_id, (map.get(_id) || 0) + 1),
new Map()
)].map(
([_id, qty]) => ({ _id, qty })
)
console.log(groups)
answered Nov 19 at 23:38
Patrick Roberts
18.4k23372
18.4k23372
add a comment |
add a comment |
The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– CertainPerformance
Nov 19 at 23:32
1
I did try a lot of answers from SO but couldn't get them to work with my particular case.
– Serks
Nov 19 at 23:51
2
That's not the same as trying to write code yourself - when you post a question, please post the code you're trying to use as well
– CertainPerformance
Nov 19 at 23:54