How to count unique value from object of array in javascript












1














I want to calculate number of unique value and put that in new array.
I have below array:



[
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
{ CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
]


I want new array with below result:



[
{ CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
{ CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
]


In this check by id, if id is same show count and new in new array.



Hope you understand what I want.



Thanks,










share|improve this question



























    1














    I want to calculate number of unique value and put that in new array.
    I have below array:



    [
    { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
    { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
    { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
    ]


    I want new array with below result:



    [
    { CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
    { CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
    ]


    In this check by id, if id is same show count and new in new array.



    Hope you understand what I want.



    Thanks,










    share|improve this question

























      1












      1








      1







      I want to calculate number of unique value and put that in new array.
      I have below array:



      [
      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
      { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
      ]


      I want new array with below result:



      [
      { CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
      { CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
      ]


      In this check by id, if id is same show count and new in new array.



      Hope you understand what I want.



      Thanks,










      share|improve this question













      I want to calculate number of unique value and put that in new array.
      I have below array:



      [
      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
      { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
      ]


      I want new array with below result:



      [
      { CategoryId: "b5c3f43f941c", count: 2, CategoryColor: "cgreen" }
      { CategoryId: "9872cce5af92", count: 1, CategoryColor: "purple" }
      ]


      In this check by id, if id is same show count and new in new array.



      Hope you understand what I want.



      Thanks,







      javascript arrays array-push






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 at 11:32









      rohit13807

      307213




      307213
























          4 Answers
          4






          active

          oldest

          votes


















          3














          You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1






          var arr = [
          { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
          , { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
          , { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
          ]

          var tempResult = {}

          for(let { CategoryColor, CategoryId } of arr)
          tempResult[CategoryId] = {
          CategoryId,
          CategoryColor,
          count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
          }

          let result = Object.values(tempResult)

          console.log(result)








          share|improve this answer































            3














            Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array






            let k = [{
            CategoryId: "b5c3f43f941c",
            CategoryName: "Category 1",
            CategoryColor: "cgreen"
            },
            {
            CategoryId: "9872cce5af92",
            CategoryName: "Category 2",
            CategoryColor: "purple"
            },
            {
            CategoryId: "b5c3f43f941c",
            CategoryName: "Category 1",
            CategoryColor: "cgreen"
            }
            ]

            let result = k.reduce(function(acc, curr) {
            // Check if there exist an object in empty array whose CategoryId matches
            let isElemExist = acc.findIndex(function(item) {
            return item.CategoryId === curr.CategoryId;
            })
            if (isElemExist === -1) {
            let obj = {};
            obj.CategoryId = curr.CategoryId;
            obj.count = 1;
            obj.CategoryColor = curr.CategoryColor;
            acc.push(obj)
            } else {
            acc[isElemExist].count += 1
            }
            return acc;

            }, )

            console.log(result)








            share|improve this answer





























              0














              const array_unique = require('array-hyper-unique').array_unique

              let arr = [
              { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
              { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
              { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
              ]

              array_unique(arr).length





              share|improve this answer





























                0














                You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.



                yourArray.sort((a, b) => {
                if (a.CategoryName > b.CategoryName) {
                return 1;
                }
                if (a.CategoryName < b.CategoryName) {
                return -1;
                }
                return 0;
                });

                var pivot = yourArray[0];
                pivot.count = 0;
                var counted = [pivot];
                yourArray.forEach(item => {
                if (item.CategoryId === pivot.CategoryId) {
                pivot.count++;
                } else {
                pivot = item;
                pivot.count = 1;
                counted.push(pivot);
                }
                });





                share|improve this answer





















                  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%2f53280115%2fhow-to-count-unique-value-from-object-of-array-in-javascript%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  3














                  You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1






                  var arr = [
                  { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                  , { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                  , { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                  ]

                  var tempResult = {}

                  for(let { CategoryColor, CategoryId } of arr)
                  tempResult[CategoryId] = {
                  CategoryId,
                  CategoryColor,
                  count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
                  }

                  let result = Object.values(tempResult)

                  console.log(result)








                  share|improve this answer




























                    3














                    You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1






                    var arr = [
                    { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                    , { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                    , { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                    ]

                    var tempResult = {}

                    for(let { CategoryColor, CategoryId } of arr)
                    tempResult[CategoryId] = {
                    CategoryId,
                    CategoryColor,
                    count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
                    }

                    let result = Object.values(tempResult)

                    console.log(result)








                    share|improve this answer


























                      3












                      3








                      3






                      You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1






                      var arr = [
                      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                      , { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                      , { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                      ]

                      var tempResult = {}

                      for(let { CategoryColor, CategoryId } of arr)
                      tempResult[CategoryId] = {
                      CategoryId,
                      CategoryColor,
                      count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
                      }

                      let result = Object.values(tempResult)

                      console.log(result)








                      share|improve this answer














                      You can loop through array using "for..of" and create a temporary object to save data in every loop. If same id exists in tempObject then increment count by 1






                      var arr = [
                      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                      , { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                      , { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                      ]

                      var tempResult = {}

                      for(let { CategoryColor, CategoryId } of arr)
                      tempResult[CategoryId] = {
                      CategoryId,
                      CategoryColor,
                      count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
                      }

                      let result = Object.values(tempResult)

                      console.log(result)








                      var arr = [
                      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                      , { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                      , { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                      ]

                      var tempResult = {}

                      for(let { CategoryColor, CategoryId } of arr)
                      tempResult[CategoryId] = {
                      CategoryId,
                      CategoryColor,
                      count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
                      }

                      let result = Object.values(tempResult)

                      console.log(result)





                      var arr = [
                      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                      , { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                      , { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                      ]

                      var tempResult = {}

                      for(let { CategoryColor, CategoryId } of arr)
                      tempResult[CategoryId] = {
                      CategoryId,
                      CategoryColor,
                      count: tempResult[CategoryId] ? tempResult[CategoryId].count + 1 : 1
                      }

                      let result = Object.values(tempResult)

                      console.log(result)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 13 at 11:47

























                      answered Nov 13 at 11:40









                      Nitish Narang

                      2,948815




                      2,948815

























                          3














                          Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array






                          let k = [{
                          CategoryId: "b5c3f43f941c",
                          CategoryName: "Category 1",
                          CategoryColor: "cgreen"
                          },
                          {
                          CategoryId: "9872cce5af92",
                          CategoryName: "Category 2",
                          CategoryColor: "purple"
                          },
                          {
                          CategoryId: "b5c3f43f941c",
                          CategoryName: "Category 1",
                          CategoryColor: "cgreen"
                          }
                          ]

                          let result = k.reduce(function(acc, curr) {
                          // Check if there exist an object in empty array whose CategoryId matches
                          let isElemExist = acc.findIndex(function(item) {
                          return item.CategoryId === curr.CategoryId;
                          })
                          if (isElemExist === -1) {
                          let obj = {};
                          obj.CategoryId = curr.CategoryId;
                          obj.count = 1;
                          obj.CategoryColor = curr.CategoryColor;
                          acc.push(obj)
                          } else {
                          acc[isElemExist].count += 1
                          }
                          return acc;

                          }, )

                          console.log(result)








                          share|improve this answer


























                            3














                            Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array






                            let k = [{
                            CategoryId: "b5c3f43f941c",
                            CategoryName: "Category 1",
                            CategoryColor: "cgreen"
                            },
                            {
                            CategoryId: "9872cce5af92",
                            CategoryName: "Category 2",
                            CategoryColor: "purple"
                            },
                            {
                            CategoryId: "b5c3f43f941c",
                            CategoryName: "Category 1",
                            CategoryColor: "cgreen"
                            }
                            ]

                            let result = k.reduce(function(acc, curr) {
                            // Check if there exist an object in empty array whose CategoryId matches
                            let isElemExist = acc.findIndex(function(item) {
                            return item.CategoryId === curr.CategoryId;
                            })
                            if (isElemExist === -1) {
                            let obj = {};
                            obj.CategoryId = curr.CategoryId;
                            obj.count = 1;
                            obj.CategoryColor = curr.CategoryColor;
                            acc.push(obj)
                            } else {
                            acc[isElemExist].count += 1
                            }
                            return acc;

                            }, )

                            console.log(result)








                            share|improve this answer
























                              3












                              3








                              3






                              Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array






                              let k = [{
                              CategoryId: "b5c3f43f941c",
                              CategoryName: "Category 1",
                              CategoryColor: "cgreen"
                              },
                              {
                              CategoryId: "9872cce5af92",
                              CategoryName: "Category 2",
                              CategoryColor: "purple"
                              },
                              {
                              CategoryId: "b5c3f43f941c",
                              CategoryName: "Category 1",
                              CategoryColor: "cgreen"
                              }
                              ]

                              let result = k.reduce(function(acc, curr) {
                              // Check if there exist an object in empty array whose CategoryId matches
                              let isElemExist = acc.findIndex(function(item) {
                              return item.CategoryId === curr.CategoryId;
                              })
                              if (isElemExist === -1) {
                              let obj = {};
                              obj.CategoryId = curr.CategoryId;
                              obj.count = 1;
                              obj.CategoryColor = curr.CategoryColor;
                              acc.push(obj)
                              } else {
                              acc[isElemExist].count += 1
                              }
                              return acc;

                              }, )

                              console.log(result)








                              share|improve this answer












                              Use reduce function and inside the callback check an object exist whose CategoryId matches. If matches then update the count or else create a new object with the values and push in the array






                              let k = [{
                              CategoryId: "b5c3f43f941c",
                              CategoryName: "Category 1",
                              CategoryColor: "cgreen"
                              },
                              {
                              CategoryId: "9872cce5af92",
                              CategoryName: "Category 2",
                              CategoryColor: "purple"
                              },
                              {
                              CategoryId: "b5c3f43f941c",
                              CategoryName: "Category 1",
                              CategoryColor: "cgreen"
                              }
                              ]

                              let result = k.reduce(function(acc, curr) {
                              // Check if there exist an object in empty array whose CategoryId matches
                              let isElemExist = acc.findIndex(function(item) {
                              return item.CategoryId === curr.CategoryId;
                              })
                              if (isElemExist === -1) {
                              let obj = {};
                              obj.CategoryId = curr.CategoryId;
                              obj.count = 1;
                              obj.CategoryColor = curr.CategoryColor;
                              acc.push(obj)
                              } else {
                              acc[isElemExist].count += 1
                              }
                              return acc;

                              }, )

                              console.log(result)








                              let k = [{
                              CategoryId: "b5c3f43f941c",
                              CategoryName: "Category 1",
                              CategoryColor: "cgreen"
                              },
                              {
                              CategoryId: "9872cce5af92",
                              CategoryName: "Category 2",
                              CategoryColor: "purple"
                              },
                              {
                              CategoryId: "b5c3f43f941c",
                              CategoryName: "Category 1",
                              CategoryColor: "cgreen"
                              }
                              ]

                              let result = k.reduce(function(acc, curr) {
                              // Check if there exist an object in empty array whose CategoryId matches
                              let isElemExist = acc.findIndex(function(item) {
                              return item.CategoryId === curr.CategoryId;
                              })
                              if (isElemExist === -1) {
                              let obj = {};
                              obj.CategoryId = curr.CategoryId;
                              obj.count = 1;
                              obj.CategoryColor = curr.CategoryColor;
                              acc.push(obj)
                              } else {
                              acc[isElemExist].count += 1
                              }
                              return acc;

                              }, )

                              console.log(result)





                              let k = [{
                              CategoryId: "b5c3f43f941c",
                              CategoryName: "Category 1",
                              CategoryColor: "cgreen"
                              },
                              {
                              CategoryId: "9872cce5af92",
                              CategoryName: "Category 2",
                              CategoryColor: "purple"
                              },
                              {
                              CategoryId: "b5c3f43f941c",
                              CategoryName: "Category 1",
                              CategoryColor: "cgreen"
                              }
                              ]

                              let result = k.reduce(function(acc, curr) {
                              // Check if there exist an object in empty array whose CategoryId matches
                              let isElemExist = acc.findIndex(function(item) {
                              return item.CategoryId === curr.CategoryId;
                              })
                              if (isElemExist === -1) {
                              let obj = {};
                              obj.CategoryId = curr.CategoryId;
                              obj.count = 1;
                              obj.CategoryColor = curr.CategoryColor;
                              acc.push(obj)
                              } else {
                              acc[isElemExist].count += 1
                              }
                              return acc;

                              }, )

                              console.log(result)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 13 at 11:40









                              brk

                              25.4k31939




                              25.4k31939























                                  0














                                  const array_unique = require('array-hyper-unique').array_unique

                                  let arr = [
                                  { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                                  { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                                  { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                                  ]

                                  array_unique(arr).length





                                  share|improve this answer


























                                    0














                                    const array_unique = require('array-hyper-unique').array_unique

                                    let arr = [
                                    { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                                    { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                                    { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                                    ]

                                    array_unique(arr).length





                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      const array_unique = require('array-hyper-unique').array_unique

                                      let arr = [
                                      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                                      { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                                      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                                      ]

                                      array_unique(arr).length





                                      share|improve this answer












                                      const array_unique = require('array-hyper-unique').array_unique

                                      let arr = [
                                      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                                      { CategoryId: "9872cce5af92", CategoryName: "Category 2", CategoryColor: "purple" }
                                      { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" }
                                      ]

                                      array_unique(arr).length






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 13 at 12:19









                                      bluelovers

                                      1538




                                      1538























                                          0














                                          You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.



                                          yourArray.sort((a, b) => {
                                          if (a.CategoryName > b.CategoryName) {
                                          return 1;
                                          }
                                          if (a.CategoryName < b.CategoryName) {
                                          return -1;
                                          }
                                          return 0;
                                          });

                                          var pivot = yourArray[0];
                                          pivot.count = 0;
                                          var counted = [pivot];
                                          yourArray.forEach(item => {
                                          if (item.CategoryId === pivot.CategoryId) {
                                          pivot.count++;
                                          } else {
                                          pivot = item;
                                          pivot.count = 1;
                                          counted.push(pivot);
                                          }
                                          });





                                          share|improve this answer


























                                            0














                                            You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.



                                            yourArray.sort((a, b) => {
                                            if (a.CategoryName > b.CategoryName) {
                                            return 1;
                                            }
                                            if (a.CategoryName < b.CategoryName) {
                                            return -1;
                                            }
                                            return 0;
                                            });

                                            var pivot = yourArray[0];
                                            pivot.count = 0;
                                            var counted = [pivot];
                                            yourArray.forEach(item => {
                                            if (item.CategoryId === pivot.CategoryId) {
                                            pivot.count++;
                                            } else {
                                            pivot = item;
                                            pivot.count = 1;
                                            counted.push(pivot);
                                            }
                                            });





                                            share|improve this answer
























                                              0












                                              0








                                              0






                                              You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.



                                              yourArray.sort((a, b) => {
                                              if (a.CategoryName > b.CategoryName) {
                                              return 1;
                                              }
                                              if (a.CategoryName < b.CategoryName) {
                                              return -1;
                                              }
                                              return 0;
                                              });

                                              var pivot = yourArray[0];
                                              pivot.count = 0;
                                              var counted = [pivot];
                                              yourArray.forEach(item => {
                                              if (item.CategoryId === pivot.CategoryId) {
                                              pivot.count++;
                                              } else {
                                              pivot = item;
                                              pivot.count = 1;
                                              counted.push(pivot);
                                              }
                                              });





                                              share|improve this answer












                                              You can sort the array first then count the duplicates. The downside is it will modify the original yourArray due to being sorted, so use with caution.



                                              yourArray.sort((a, b) => {
                                              if (a.CategoryName > b.CategoryName) {
                                              return 1;
                                              }
                                              if (a.CategoryName < b.CategoryName) {
                                              return -1;
                                              }
                                              return 0;
                                              });

                                              var pivot = yourArray[0];
                                              pivot.count = 0;
                                              var counted = [pivot];
                                              yourArray.forEach(item => {
                                              if (item.CategoryId === pivot.CategoryId) {
                                              pivot.count++;
                                              } else {
                                              pivot = item;
                                              pivot.count = 1;
                                              counted.push(pivot);
                                              }
                                              });






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 13 at 13:43









                                              ptdien

                                              744




                                              744






























                                                  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.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • 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%2f53280115%2fhow-to-count-unique-value-from-object-of-array-in-javascript%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

                                                  How to pass form data using jquery Ajax to insert data in database?

                                                  National Museum of Racing and Hall of Fame

                                                  Guess what letter conforming each word