Filtering duplicate hashes from array of hashes - Javascript












8














I have an array of hashes, like this:



[{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c6941735", name: "skandi"},
{id: "4bf58dd8d48988d147941735", name: "diner"},
{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d14a941735", name: "vietnam"},
{id: "4bf58dd8d48988d1ce941735", name: "fish"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"}]


I want to throw out duplicate hashes. Set doesn't work because hashes are unique objects.



I feel stuck and need a kick to think. Please advise!










share|improve this question




















  • 2




    Reduce the array to an object, using the id as key, and then convert back to array using Object.values().
    – Ori Drori
    Dec 21 at 9:44










  • What you tried?
    – ZiTAL
    Dec 21 at 9:45










  • You would have to loop through the array for each possible hash and check, remove. Or change the data format, use an object with the hash string as a key and the name and whatever else as a separate object for the value
    – Patrick Evans
    Dec 21 at 9:46












  • Only pure JS allowed?
    – hindmost
    Dec 21 at 9:49










  • @hindmost, yes, this is part of my react app
    – Anna Kizilova
    Dec 21 at 9:52
















8














I have an array of hashes, like this:



[{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c6941735", name: "skandi"},
{id: "4bf58dd8d48988d147941735", name: "diner"},
{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d14a941735", name: "vietnam"},
{id: "4bf58dd8d48988d1ce941735", name: "fish"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"}]


I want to throw out duplicate hashes. Set doesn't work because hashes are unique objects.



I feel stuck and need a kick to think. Please advise!










share|improve this question




















  • 2




    Reduce the array to an object, using the id as key, and then convert back to array using Object.values().
    – Ori Drori
    Dec 21 at 9:44










  • What you tried?
    – ZiTAL
    Dec 21 at 9:45










  • You would have to loop through the array for each possible hash and check, remove. Or change the data format, use an object with the hash string as a key and the name and whatever else as a separate object for the value
    – Patrick Evans
    Dec 21 at 9:46












  • Only pure JS allowed?
    – hindmost
    Dec 21 at 9:49










  • @hindmost, yes, this is part of my react app
    – Anna Kizilova
    Dec 21 at 9:52














8












8








8







I have an array of hashes, like this:



[{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c6941735", name: "skandi"},
{id: "4bf58dd8d48988d147941735", name: "diner"},
{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d14a941735", name: "vietnam"},
{id: "4bf58dd8d48988d1ce941735", name: "fish"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"}]


I want to throw out duplicate hashes. Set doesn't work because hashes are unique objects.



I feel stuck and need a kick to think. Please advise!










share|improve this question















I have an array of hashes, like this:



[{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c6941735", name: "skandi"},
{id: "4bf58dd8d48988d147941735", name: "diner"},
{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d14a941735", name: "vietnam"},
{id: "4bf58dd8d48988d1ce941735", name: "fish"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"}]


I want to throw out duplicate hashes. Set doesn't work because hashes are unique objects.



I feel stuck and need a kick to think. Please advise!







javascript arrays hash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 21 at 11:07









Kamil Kiełczewski

8,82585688




8,82585688










asked Dec 21 at 9:40









Anna Kizilova

8319




8319








  • 2




    Reduce the array to an object, using the id as key, and then convert back to array using Object.values().
    – Ori Drori
    Dec 21 at 9:44










  • What you tried?
    – ZiTAL
    Dec 21 at 9:45










  • You would have to loop through the array for each possible hash and check, remove. Or change the data format, use an object with the hash string as a key and the name and whatever else as a separate object for the value
    – Patrick Evans
    Dec 21 at 9:46












  • Only pure JS allowed?
    – hindmost
    Dec 21 at 9:49










  • @hindmost, yes, this is part of my react app
    – Anna Kizilova
    Dec 21 at 9:52














  • 2




    Reduce the array to an object, using the id as key, and then convert back to array using Object.values().
    – Ori Drori
    Dec 21 at 9:44










  • What you tried?
    – ZiTAL
    Dec 21 at 9:45










  • You would have to loop through the array for each possible hash and check, remove. Or change the data format, use an object with the hash string as a key and the name and whatever else as a separate object for the value
    – Patrick Evans
    Dec 21 at 9:46












  • Only pure JS allowed?
    – hindmost
    Dec 21 at 9:49










  • @hindmost, yes, this is part of my react app
    – Anna Kizilova
    Dec 21 at 9:52








2




2




Reduce the array to an object, using the id as key, and then convert back to array using Object.values().
– Ori Drori
Dec 21 at 9:44




Reduce the array to an object, using the id as key, and then convert back to array using Object.values().
– Ori Drori
Dec 21 at 9:44












What you tried?
– ZiTAL
Dec 21 at 9:45




What you tried?
– ZiTAL
Dec 21 at 9:45












You would have to loop through the array for each possible hash and check, remove. Or change the data format, use an object with the hash string as a key and the name and whatever else as a separate object for the value
– Patrick Evans
Dec 21 at 9:46






You would have to loop through the array for each possible hash and check, remove. Or change the data format, use an object with the hash string as a key and the name and whatever else as a separate object for the value
– Patrick Evans
Dec 21 at 9:46














Only pure JS allowed?
– hindmost
Dec 21 at 9:49




Only pure JS allowed?
– hindmost
Dec 21 at 9:49












@hindmost, yes, this is part of my react app
– Anna Kizilova
Dec 21 at 9:52




@hindmost, yes, this is part of my react app
– Anna Kizilova
Dec 21 at 9:52












5 Answers
5






active

oldest

votes


















6














You can use reduce too






//I added comma to each object
const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c6941735", name: "skandi"},
{id: "4bf58dd8d48988d147941735", name: "diner"},
{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d14a941735", name: "vietnam"},
{id: "4bf58dd8d48988d1ce941735", name: "fish"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"}]

const result= data.reduce((current,next)=>{
if(!current.some(a=> a.name === next.name)){
current.push(next);
}
return current;
},)
console.log(result);








share|improve this answer































    4














    Try this



    h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


    Input array in h, time complexity O(n), explanation here.






    let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
    {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
    {id: "4bf58dd8d48988d147941735", name: "diner"},
    {id: "4bf58dd8d48988d110941735", name: "italy"},
    {id: "4bf58dd8d48988d1c4941735", name: "resto"},
    {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
    {id: "4bf58dd8d48988d1ce941735", name: "fish"},
    {id: "4bf58dd8d48988d1c4941735", name: "resto"},
    {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

    let t; // declare t to avoid use global (however works without it too)
    let r= h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


    console.log(JSON.stringify(r));








    share|improve this answer































      3














      Here. A little bit messy, but gets the job done.






      let array = [{id: "4bf58dd8d48988d110941735", name: "italy"},
      {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
      {id: "4bf58dd8d48988d147941735", name: "diner"},
      {id: "4bf58dd8d48988d110941735", name: "italy"},
      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
      {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
      {id: "4bf58dd8d48988d1ce941735", name: "fish"},
      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
      {id: "4bf58dd8d48988d1c4941735", name: "resto"}];
      let result = ;
      let duplicate;
      array.forEach(function(a){
      duplicate = false;
      result.forEach(function(r){
      if (a.name === r.name){
      duplicate = true;
      }
      });
      if (!duplicate) {
      result.push(a);
      }
      });
      console.log(result);








      share|improve this answer





























        3














        Space for time






        let arr = [
        { id: '4bf58dd8d48988d110941735', name: 'italy' },
        { id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
        { id: '4bf58dd8d48988d147941735', name: 'diner' },
        { id: '4bf58dd8d48988d110941735', name: 'italy' },
        { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
        { id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
        { id: '4bf58dd8d48988d1ce941735', name: 'fish' },
        { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
        { id: '4bf58dd8d48988d1c4941735', name: 'resto' }
        ]

        let map = {};
        let rest = arr.filter((item) => {
        if(map[item.id] === void 0) {
        map[item.id] = item.id;
        return true;
        }
        });
        map = null;

        console.log(rest);








        share|improve this answer





























          2














          I would suggest an approach with associative arrays, this makes duplicate removal easier. If you can, you should build your array as an associative array in the first place, so that you don't have to convert it. Here is how you do it:






          var array = [{
          id: "4bf58dd8d48988d110941735",
          name: "italy"
          },
          {
          id: "4bf58dd8d48988d1c6941735",
          name: "skandi"
          }, {
          id: "4bf58dd8d48988d147941735",
          name: "diner"
          }, {
          id: "4bf58dd8d48988d110941735",
          name: "italy"
          }, {
          id: "4bf58dd8d48988d1c4941735",
          name: "resto"
          }, {
          id: "4bf58dd8d48988d14a941735",
          name: "vietnam"
          }, {
          id: "4bf58dd8d48988d14a941735",
          name: "fish"
          }, {
          id: "4bf58dd8d48988d1c4941735",
          name: "resto"
          }, {
          id: "4bf58dd8d48988d1c4941735",
          name: "resto"
          }
          ];

          // you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
          var arrayAssociative = {};
          for (item in array) {
          // first get the unique id's
          var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
          if (addedNode.names == null)
          addedNode.names = {};
          // now get the unique names
          var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
          }
          console.log(arrayAssociative);





          I don't know the exact reason, why the line




          var element = arrayAssociative[id] =arrayAssociative[id] || {};




          works for this, but let's just accept the funcitonality as it is :)






          share|improve this answer



















          • 1




            arrayAssociative[array[item].id] || {} this line gives value in left of || if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
            – Kamil Kiełczewski
            Dec 21 at 10:03










          • thanks for the explanation <3
            – Stephan T.
            Dec 21 at 10:05











          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%2f53882375%2ffiltering-duplicate-hashes-from-array-of-hashes-javascript%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          You can use reduce too






          //I added comma to each object
          const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
          {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
          {id: "4bf58dd8d48988d147941735", name: "diner"},
          {id: "4bf58dd8d48988d110941735", name: "italy"},
          {id: "4bf58dd8d48988d1c4941735", name: "resto"},
          {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
          {id: "4bf58dd8d48988d1ce941735", name: "fish"},
          {id: "4bf58dd8d48988d1c4941735", name: "resto"},
          {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

          const result= data.reduce((current,next)=>{
          if(!current.some(a=> a.name === next.name)){
          current.push(next);
          }
          return current;
          },)
          console.log(result);








          share|improve this answer




























            6














            You can use reduce too






            //I added comma to each object
            const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
            {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
            {id: "4bf58dd8d48988d147941735", name: "diner"},
            {id: "4bf58dd8d48988d110941735", name: "italy"},
            {id: "4bf58dd8d48988d1c4941735", name: "resto"},
            {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
            {id: "4bf58dd8d48988d1ce941735", name: "fish"},
            {id: "4bf58dd8d48988d1c4941735", name: "resto"},
            {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

            const result= data.reduce((current,next)=>{
            if(!current.some(a=> a.name === next.name)){
            current.push(next);
            }
            return current;
            },)
            console.log(result);








            share|improve this answer


























              6












              6








              6






              You can use reduce too






              //I added comma to each object
              const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
              {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
              {id: "4bf58dd8d48988d147941735", name: "diner"},
              {id: "4bf58dd8d48988d110941735", name: "italy"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
              {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
              {id: "4bf58dd8d48988d1ce941735", name: "fish"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

              const result= data.reduce((current,next)=>{
              if(!current.some(a=> a.name === next.name)){
              current.push(next);
              }
              return current;
              },)
              console.log(result);








              share|improve this answer














              You can use reduce too






              //I added comma to each object
              const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
              {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
              {id: "4bf58dd8d48988d147941735", name: "diner"},
              {id: "4bf58dd8d48988d110941735", name: "italy"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
              {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
              {id: "4bf58dd8d48988d1ce941735", name: "fish"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

              const result= data.reduce((current,next)=>{
              if(!current.some(a=> a.name === next.name)){
              current.push(next);
              }
              return current;
              },)
              console.log(result);








              //I added comma to each object
              const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
              {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
              {id: "4bf58dd8d48988d147941735", name: "diner"},
              {id: "4bf58dd8d48988d110941735", name: "italy"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
              {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
              {id: "4bf58dd8d48988d1ce941735", name: "fish"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

              const result= data.reduce((current,next)=>{
              if(!current.some(a=> a.name === next.name)){
              current.push(next);
              }
              return current;
              },)
              console.log(result);





              //I added comma to each object
              const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
              {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
              {id: "4bf58dd8d48988d147941735", name: "diner"},
              {id: "4bf58dd8d48988d110941735", name: "italy"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
              {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
              {id: "4bf58dd8d48988d1ce941735", name: "fish"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
              {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

              const result= data.reduce((current,next)=>{
              if(!current.some(a=> a.name === next.name)){
              current.push(next);
              }
              return current;
              },)
              console.log(result);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 21 at 11:50

























              answered Dec 21 at 10:04









              Just code

              8,27642966




              8,27642966

























                  4














                  Try this



                  h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                  Input array in h, time complexity O(n), explanation here.






                  let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                  {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                  {id: "4bf58dd8d48988d147941735", name: "diner"},
                  {id: "4bf58dd8d48988d110941735", name: "italy"},
                  {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                  {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                  {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                  {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                  {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

                  let t; // declare t to avoid use global (however works without it too)
                  let r= h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                  console.log(JSON.stringify(r));








                  share|improve this answer




























                    4














                    Try this



                    h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                    Input array in h, time complexity O(n), explanation here.






                    let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                    {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                    {id: "4bf58dd8d48988d147941735", name: "diner"},
                    {id: "4bf58dd8d48988d110941735", name: "italy"},
                    {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                    {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                    {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                    {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                    {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

                    let t; // declare t to avoid use global (however works without it too)
                    let r= h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                    console.log(JSON.stringify(r));








                    share|improve this answer


























                      4












                      4








                      4






                      Try this



                      h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                      Input array in h, time complexity O(n), explanation here.






                      let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                      {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                      {id: "4bf58dd8d48988d147941735", name: "diner"},
                      {id: "4bf58dd8d48988d110941735", name: "italy"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                      {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                      {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

                      let t; // declare t to avoid use global (however works without it too)
                      let r= h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                      console.log(JSON.stringify(r));








                      share|improve this answer














                      Try this



                      h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                      Input array in h, time complexity O(n), explanation here.






                      let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                      {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                      {id: "4bf58dd8d48988d147941735", name: "diner"},
                      {id: "4bf58dd8d48988d110941735", name: "italy"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                      {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                      {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

                      let t; // declare t to avoid use global (however works without it too)
                      let r= h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                      console.log(JSON.stringify(r));








                      let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                      {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                      {id: "4bf58dd8d48988d147941735", name: "diner"},
                      {id: "4bf58dd8d48988d110941735", name: "italy"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                      {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                      {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

                      let t; // declare t to avoid use global (however works without it too)
                      let r= h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                      console.log(JSON.stringify(r));





                      let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                      {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                      {id: "4bf58dd8d48988d147941735", name: "diner"},
                      {id: "4bf58dd8d48988d110941735", name: "italy"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                      {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                      {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                      {id: "4bf58dd8d48988d1c4941735", name: "resto"}]

                      let t; // declare t to avoid use global (however works without it too)
                      let r= h.filter(( t={}, a=>!(t[a.id]=++t[a.id]|0) ))


                      console.log(JSON.stringify(r));






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited yesterday

























                      answered Dec 21 at 9:45









                      Kamil Kiełczewski

                      8,82585688




                      8,82585688























                          3














                          Here. A little bit messy, but gets the job done.






                          let array = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                          {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                          {id: "4bf58dd8d48988d147941735", name: "diner"},
                          {id: "4bf58dd8d48988d110941735", name: "italy"},
                          {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                          {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                          {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                          {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                          {id: "4bf58dd8d48988d1c4941735", name: "resto"}];
                          let result = ;
                          let duplicate;
                          array.forEach(function(a){
                          duplicate = false;
                          result.forEach(function(r){
                          if (a.name === r.name){
                          duplicate = true;
                          }
                          });
                          if (!duplicate) {
                          result.push(a);
                          }
                          });
                          console.log(result);








                          share|improve this answer


























                            3














                            Here. A little bit messy, but gets the job done.






                            let array = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                            {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                            {id: "4bf58dd8d48988d147941735", name: "diner"},
                            {id: "4bf58dd8d48988d110941735", name: "italy"},
                            {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                            {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                            {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                            {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                            {id: "4bf58dd8d48988d1c4941735", name: "resto"}];
                            let result = ;
                            let duplicate;
                            array.forEach(function(a){
                            duplicate = false;
                            result.forEach(function(r){
                            if (a.name === r.name){
                            duplicate = true;
                            }
                            });
                            if (!duplicate) {
                            result.push(a);
                            }
                            });
                            console.log(result);








                            share|improve this answer
























                              3












                              3








                              3






                              Here. A little bit messy, but gets the job done.






                              let array = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                              {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                              {id: "4bf58dd8d48988d147941735", name: "diner"},
                              {id: "4bf58dd8d48988d110941735", name: "italy"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                              {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                              {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"}];
                              let result = ;
                              let duplicate;
                              array.forEach(function(a){
                              duplicate = false;
                              result.forEach(function(r){
                              if (a.name === r.name){
                              duplicate = true;
                              }
                              });
                              if (!duplicate) {
                              result.push(a);
                              }
                              });
                              console.log(result);








                              share|improve this answer












                              Here. A little bit messy, but gets the job done.






                              let array = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                              {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                              {id: "4bf58dd8d48988d147941735", name: "diner"},
                              {id: "4bf58dd8d48988d110941735", name: "italy"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                              {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                              {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"}];
                              let result = ;
                              let duplicate;
                              array.forEach(function(a){
                              duplicate = false;
                              result.forEach(function(r){
                              if (a.name === r.name){
                              duplicate = true;
                              }
                              });
                              if (!duplicate) {
                              result.push(a);
                              }
                              });
                              console.log(result);








                              let array = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                              {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                              {id: "4bf58dd8d48988d147941735", name: "diner"},
                              {id: "4bf58dd8d48988d110941735", name: "italy"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                              {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                              {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"}];
                              let result = ;
                              let duplicate;
                              array.forEach(function(a){
                              duplicate = false;
                              result.forEach(function(r){
                              if (a.name === r.name){
                              duplicate = true;
                              }
                              });
                              if (!duplicate) {
                              result.push(a);
                              }
                              });
                              console.log(result);





                              let array = [{id: "4bf58dd8d48988d110941735", name: "italy"},
                              {id: "4bf58dd8d48988d1c6941735", name: "skandi"},
                              {id: "4bf58dd8d48988d147941735", name: "diner"},
                              {id: "4bf58dd8d48988d110941735", name: "italy"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                              {id: "4bf58dd8d48988d14a941735", name: "vietnam"},
                              {id: "4bf58dd8d48988d1ce941735", name: "fish"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"},
                              {id: "4bf58dd8d48988d1c4941735", name: "resto"}];
                              let result = ;
                              let duplicate;
                              array.forEach(function(a){
                              duplicate = false;
                              result.forEach(function(r){
                              if (a.name === r.name){
                              duplicate = true;
                              }
                              });
                              if (!duplicate) {
                              result.push(a);
                              }
                              });
                              console.log(result);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 21 at 9:51









                              holydragon

                              1,1531618




                              1,1531618























                                  3














                                  Space for time






                                  let arr = [
                                  { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                  { id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
                                  { id: '4bf58dd8d48988d147941735', name: 'diner' },
                                  { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                  { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                  { id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
                                  { id: '4bf58dd8d48988d1ce941735', name: 'fish' },
                                  { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                  { id: '4bf58dd8d48988d1c4941735', name: 'resto' }
                                  ]

                                  let map = {};
                                  let rest = arr.filter((item) => {
                                  if(map[item.id] === void 0) {
                                  map[item.id] = item.id;
                                  return true;
                                  }
                                  });
                                  map = null;

                                  console.log(rest);








                                  share|improve this answer


























                                    3














                                    Space for time






                                    let arr = [
                                    { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                    { id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
                                    { id: '4bf58dd8d48988d147941735', name: 'diner' },
                                    { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                    { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                    { id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
                                    { id: '4bf58dd8d48988d1ce941735', name: 'fish' },
                                    { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                    { id: '4bf58dd8d48988d1c4941735', name: 'resto' }
                                    ]

                                    let map = {};
                                    let rest = arr.filter((item) => {
                                    if(map[item.id] === void 0) {
                                    map[item.id] = item.id;
                                    return true;
                                    }
                                    });
                                    map = null;

                                    console.log(rest);








                                    share|improve this answer
























                                      3












                                      3








                                      3






                                      Space for time






                                      let arr = [
                                      { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                      { id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
                                      { id: '4bf58dd8d48988d147941735', name: 'diner' },
                                      { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                      { id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
                                      { id: '4bf58dd8d48988d1ce941735', name: 'fish' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' }
                                      ]

                                      let map = {};
                                      let rest = arr.filter((item) => {
                                      if(map[item.id] === void 0) {
                                      map[item.id] = item.id;
                                      return true;
                                      }
                                      });
                                      map = null;

                                      console.log(rest);








                                      share|improve this answer












                                      Space for time






                                      let arr = [
                                      { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                      { id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
                                      { id: '4bf58dd8d48988d147941735', name: 'diner' },
                                      { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                      { id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
                                      { id: '4bf58dd8d48988d1ce941735', name: 'fish' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' }
                                      ]

                                      let map = {};
                                      let rest = arr.filter((item) => {
                                      if(map[item.id] === void 0) {
                                      map[item.id] = item.id;
                                      return true;
                                      }
                                      });
                                      map = null;

                                      console.log(rest);








                                      let arr = [
                                      { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                      { id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
                                      { id: '4bf58dd8d48988d147941735', name: 'diner' },
                                      { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                      { id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
                                      { id: '4bf58dd8d48988d1ce941735', name: 'fish' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' }
                                      ]

                                      let map = {};
                                      let rest = arr.filter((item) => {
                                      if(map[item.id] === void 0) {
                                      map[item.id] = item.id;
                                      return true;
                                      }
                                      });
                                      map = null;

                                      console.log(rest);





                                      let arr = [
                                      { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                      { id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
                                      { id: '4bf58dd8d48988d147941735', name: 'diner' },
                                      { id: '4bf58dd8d48988d110941735', name: 'italy' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                      { id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
                                      { id: '4bf58dd8d48988d1ce941735', name: 'fish' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' },
                                      { id: '4bf58dd8d48988d1c4941735', name: 'resto' }
                                      ]

                                      let map = {};
                                      let rest = arr.filter((item) => {
                                      if(map[item.id] === void 0) {
                                      map[item.id] = item.id;
                                      return true;
                                      }
                                      });
                                      map = null;

                                      console.log(rest);






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 21 at 10:03









                                      zheng li

                                      547




                                      547























                                          2














                                          I would suggest an approach with associative arrays, this makes duplicate removal easier. If you can, you should build your array as an associative array in the first place, so that you don't have to convert it. Here is how you do it:






                                          var array = [{
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          },
                                          {
                                          id: "4bf58dd8d48988d1c6941735",
                                          name: "skandi"
                                          }, {
                                          id: "4bf58dd8d48988d147941735",
                                          name: "diner"
                                          }, {
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "vietnam"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "fish"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }
                                          ];

                                          // you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
                                          var arrayAssociative = {};
                                          for (item in array) {
                                          // first get the unique id's
                                          var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
                                          if (addedNode.names == null)
                                          addedNode.names = {};
                                          // now get the unique names
                                          var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
                                          }
                                          console.log(arrayAssociative);





                                          I don't know the exact reason, why the line




                                          var element = arrayAssociative[id] =arrayAssociative[id] || {};




                                          works for this, but let's just accept the funcitonality as it is :)






                                          share|improve this answer



















                                          • 1




                                            arrayAssociative[array[item].id] || {} this line gives value in left of || if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
                                            – Kamil Kiełczewski
                                            Dec 21 at 10:03










                                          • thanks for the explanation <3
                                            – Stephan T.
                                            Dec 21 at 10:05
















                                          2














                                          I would suggest an approach with associative arrays, this makes duplicate removal easier. If you can, you should build your array as an associative array in the first place, so that you don't have to convert it. Here is how you do it:






                                          var array = [{
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          },
                                          {
                                          id: "4bf58dd8d48988d1c6941735",
                                          name: "skandi"
                                          }, {
                                          id: "4bf58dd8d48988d147941735",
                                          name: "diner"
                                          }, {
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "vietnam"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "fish"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }
                                          ];

                                          // you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
                                          var arrayAssociative = {};
                                          for (item in array) {
                                          // first get the unique id's
                                          var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
                                          if (addedNode.names == null)
                                          addedNode.names = {};
                                          // now get the unique names
                                          var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
                                          }
                                          console.log(arrayAssociative);





                                          I don't know the exact reason, why the line




                                          var element = arrayAssociative[id] =arrayAssociative[id] || {};




                                          works for this, but let's just accept the funcitonality as it is :)






                                          share|improve this answer



















                                          • 1




                                            arrayAssociative[array[item].id] || {} this line gives value in left of || if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
                                            – Kamil Kiełczewski
                                            Dec 21 at 10:03










                                          • thanks for the explanation <3
                                            – Stephan T.
                                            Dec 21 at 10:05














                                          2












                                          2








                                          2






                                          I would suggest an approach with associative arrays, this makes duplicate removal easier. If you can, you should build your array as an associative array in the first place, so that you don't have to convert it. Here is how you do it:






                                          var array = [{
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          },
                                          {
                                          id: "4bf58dd8d48988d1c6941735",
                                          name: "skandi"
                                          }, {
                                          id: "4bf58dd8d48988d147941735",
                                          name: "diner"
                                          }, {
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "vietnam"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "fish"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }
                                          ];

                                          // you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
                                          var arrayAssociative = {};
                                          for (item in array) {
                                          // first get the unique id's
                                          var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
                                          if (addedNode.names == null)
                                          addedNode.names = {};
                                          // now get the unique names
                                          var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
                                          }
                                          console.log(arrayAssociative);





                                          I don't know the exact reason, why the line




                                          var element = arrayAssociative[id] =arrayAssociative[id] || {};




                                          works for this, but let's just accept the funcitonality as it is :)






                                          share|improve this answer














                                          I would suggest an approach with associative arrays, this makes duplicate removal easier. If you can, you should build your array as an associative array in the first place, so that you don't have to convert it. Here is how you do it:






                                          var array = [{
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          },
                                          {
                                          id: "4bf58dd8d48988d1c6941735",
                                          name: "skandi"
                                          }, {
                                          id: "4bf58dd8d48988d147941735",
                                          name: "diner"
                                          }, {
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "vietnam"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "fish"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }
                                          ];

                                          // you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
                                          var arrayAssociative = {};
                                          for (item in array) {
                                          // first get the unique id's
                                          var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
                                          if (addedNode.names == null)
                                          addedNode.names = {};
                                          // now get the unique names
                                          var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
                                          }
                                          console.log(arrayAssociative);





                                          I don't know the exact reason, why the line




                                          var element = arrayAssociative[id] =arrayAssociative[id] || {};




                                          works for this, but let's just accept the funcitonality as it is :)






                                          var array = [{
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          },
                                          {
                                          id: "4bf58dd8d48988d1c6941735",
                                          name: "skandi"
                                          }, {
                                          id: "4bf58dd8d48988d147941735",
                                          name: "diner"
                                          }, {
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "vietnam"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "fish"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }
                                          ];

                                          // you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
                                          var arrayAssociative = {};
                                          for (item in array) {
                                          // first get the unique id's
                                          var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
                                          if (addedNode.names == null)
                                          addedNode.names = {};
                                          // now get the unique names
                                          var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
                                          }
                                          console.log(arrayAssociative);





                                          var array = [{
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          },
                                          {
                                          id: "4bf58dd8d48988d1c6941735",
                                          name: "skandi"
                                          }, {
                                          id: "4bf58dd8d48988d147941735",
                                          name: "diner"
                                          }, {
                                          id: "4bf58dd8d48988d110941735",
                                          name: "italy"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "vietnam"
                                          }, {
                                          id: "4bf58dd8d48988d14a941735",
                                          name: "fish"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }, {
                                          id: "4bf58dd8d48988d1c4941735",
                                          name: "resto"
                                          }
                                          ];

                                          // you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
                                          var arrayAssociative = {};
                                          for (item in array) {
                                          // first get the unique id's
                                          var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
                                          if (addedNode.names == null)
                                          addedNode.names = {};
                                          // now get the unique names
                                          var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
                                          }
                                          console.log(arrayAssociative);






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Dec 21 at 10:00

























                                          answered Dec 21 at 9:52









                                          Stephan T.

                                          649620




                                          649620








                                          • 1




                                            arrayAssociative[array[item].id] || {} this line gives value in left of || if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
                                            – Kamil Kiełczewski
                                            Dec 21 at 10:03










                                          • thanks for the explanation <3
                                            – Stephan T.
                                            Dec 21 at 10:05














                                          • 1




                                            arrayAssociative[array[item].id] || {} this line gives value in left of || if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
                                            – Kamil Kiełczewski
                                            Dec 21 at 10:03










                                          • thanks for the explanation <3
                                            – Stephan T.
                                            Dec 21 at 10:05








                                          1




                                          1




                                          arrayAssociative[array[item].id] || {} this line gives value in left of || if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
                                          – Kamil Kiełczewski
                                          Dec 21 at 10:03




                                          arrayAssociative[array[item].id] || {} this line gives value in left of || if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
                                          – Kamil Kiełczewski
                                          Dec 21 at 10:03












                                          thanks for the explanation <3
                                          – Stephan T.
                                          Dec 21 at 10:05




                                          thanks for the explanation <3
                                          – Stephan T.
                                          Dec 21 at 10:05


















                                          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%2f53882375%2ffiltering-duplicate-hashes-from-array-of-hashes-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

                                          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                          Alcedinidae

                                          Can an atomic nucleus contain both particles and antiparticles? [duplicate]