Javascript: Group similar items in array based on a value [duplicate]











up vote
0
down vote

favorite
1













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?










share|improve this question













marked as duplicate by slider, Akrion, Phil javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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















up vote
0
down vote

favorite
1













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?










share|improve this question













marked as duplicate by slider, Akrion, Phil javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1






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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 23:32









Serks

8712




8712




marked as duplicate by slider, Akrion, Phil javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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


















  • 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












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)








share|improve this answer




























    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)








    share|improve this answer

























      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)








      share|improve this answer























        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)








        share|improve this answer












        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 23:38









        Patrick Roberts

        18.4k23372




        18.4k23372















            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'