Filtering duplicate hashes from array of hashes - Javascript












10















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 '18 at 9:44











  • What you tried?

    – ZiTAL
    Dec 21 '18 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 '18 at 9:46













  • Only pure JS allowed?

    – hindmost
    Dec 21 '18 at 9:49











  • @hindmost, yes, this is part of my react app

    – Dende
    Dec 21 '18 at 9:52
















10















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 '18 at 9:44











  • What you tried?

    – ZiTAL
    Dec 21 '18 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 '18 at 9:46













  • Only pure JS allowed?

    – hindmost
    Dec 21 '18 at 9:49











  • @hindmost, yes, this is part of my react app

    – Dende
    Dec 21 '18 at 9:52














10












10








10


1






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 '18 at 11:07









Kamil Kiełczewski

13.4k87297




13.4k87297










asked Dec 21 '18 at 9:40









DendeDende

103210




103210








  • 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 '18 at 9:44











  • What you tried?

    – ZiTAL
    Dec 21 '18 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 '18 at 9:46













  • Only pure JS allowed?

    – hindmost
    Dec 21 '18 at 9:49











  • @hindmost, yes, this is part of my react app

    – Dende
    Dec 21 '18 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 '18 at 9:44











  • What you tried?

    – ZiTAL
    Dec 21 '18 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 '18 at 9:46













  • Only pure JS allowed?

    – hindmost
    Dec 21 '18 at 9:49











  • @hindmost, yes, this is part of my react app

    – Dende
    Dec 21 '18 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 '18 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 '18 at 9:44













What you tried?

– ZiTAL
Dec 21 '18 at 9:45





What you tried?

– ZiTAL
Dec 21 '18 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 '18 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 '18 at 9:46















Only pure JS allowed?

– hindmost
Dec 21 '18 at 9:49





Only pure JS allowed?

– hindmost
Dec 21 '18 at 9:49













@hindmost, yes, this is part of my react app

– Dende
Dec 21 '18 at 9:52





@hindmost, yes, this is part of my react app

– Dende
Dec 21 '18 at 9:52












4 Answers
4






active

oldest

votes


















7














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

































    5














    Try this



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


    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]=a.id in t) ))


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








    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 '18 at 10:03











        • thanks for the explanation <3

          – Stephan T.
          Dec 21 '18 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

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        7














        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






























          7














          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




























            7












            7








            7







            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 '18 at 11:50

























            answered Dec 21 '18 at 10:04









            Just codeJust code

            10.5k53267




            10.5k53267

























                5














                Try this



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


                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]=a.id in t) ))


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








                share|improve this answer






























                  5














                  Try this



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


                  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]=a.id in t) ))


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








                  share|improve this answer




























                    5












                    5








                    5







                    Try this



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


                    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]=a.id in t) ))


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








                    share|improve this answer















                    Try this



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


                    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]=a.id in t) ))


                    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]=a.id in t) ))


                    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]=a.id in t) ))


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






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 14 at 3:41

























                    answered Dec 21 '18 at 9:45









                    Kamil KiełczewskiKamil Kiełczewski

                    13.4k87297




                    13.4k87297























                        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 '18 at 10:03









                            zheng lizheng li

                            6917




                            6917























                                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 '18 at 10:03











                                • thanks for the explanation <3

                                  – Stephan T.
                                  Dec 21 '18 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 '18 at 10:03











                                • thanks for the explanation <3

                                  – Stephan T.
                                  Dec 21 '18 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 '18 at 10:00

























                                answered Dec 21 '18 at 9:52









                                Stephan T.Stephan T.

                                1,2821921




                                1,2821921








                                • 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 '18 at 10:03











                                • thanks for the explanation <3

                                  – Stephan T.
                                  Dec 21 '18 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 '18 at 10:03











                                • thanks for the explanation <3

                                  – Stephan T.
                                  Dec 21 '18 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 '18 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 '18 at 10:03













                                thanks for the explanation <3

                                – Stephan T.
                                Dec 21 '18 at 10:05





                                thanks for the explanation <3

                                – Stephan T.
                                Dec 21 '18 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.




                                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

                                Le Mesnil-Réaume

                                Ida-Boy-Ed-Garten

                                web3.py web3.isConnected() returns false always