-1

I have the following code which tries to remove duplicates from a randomly selected array items but it did'nt work,

items[] is the array containing its and i have usea sript to remove duplicates.

how do i change it to remove duplicates

what is the mistake?

window.onload = rnumber();
function rnumber() {
  const
    items = [  
      { label: '1', url: '1.jpg'  },
      { label: '2', url: '2.jpg'  },
      { label: '3', url: '3.jpg'  },
      { label: '4', url: '4.jpg'  },
      { label: '5', url: '5.jpg'  },
      { label: '6', url: '6.jpg'  },
      { label: '7', url: '7.jpg'  },
      { label: '8', url: '8.jpg'  },
      { label: '9', url: '9.jpg'  },
      { label: '10',url: '10.jpg' }
          ];

  
var lastnumber=0;
for (let index = 0; index < 9; index++) 
  {
      randomIndex = Math.floor(Math.random() * items.length);
      
  if(lastnumber!=randomIndex)
  {
      item = items[randomIndex];
      lastnumber=randomIndex;
   
   console.log(randomIndex);
   }
   else
   {
   rnumber()
   }
     
  }

}
geek
  • 205
  • 1
  • 9
  • remove comma after array and put semicolon there. its throws error. – Arif Rathod Sep 18 '18 at 05:29
  • Possible duplicate of [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – RoadEx Sep 18 '18 at 07:20

3 Answers3

1

You can use reduce to get rid of duplicates

const distinctShuffle = array =>
  array ? array.reduce((arr, item) => {
    if (!(item in arr)) {
      arr.splice(Math.floor(Math.random() * (arr.length + 1)), 0, item);
    }
    return arr;
  }, []) : array;


function rnumber() {
  const items = [  
      { label: '1', url: '1.jpg'  },
      { label: '2', url: '2.jpg'  },
      { label: '3', url: '3.jpg'  },
      { label: '4', url: '4.jpg'  },
      { label: '5', url: '5.jpg'  },
      { label: '6', url: '6.jpg'  },
      { label: '7', url: '7.jpg'  },
      { label: '8', url: '8.jpg'  },
      { label: '9', url: '9.jpg'  },
      { label: '10',url: '10.jpg' }
          ];

  
      return distinctShuffle(items);
}

console.log(rnumber());
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
0

You have some syntax errors in your code. Watch out for using semicolons at the end of the statement - not commas.

If you're using ES6 you can write something like this:

let distinct = [...new Set(items.map(item => item.label))]
Jonasz
  • 21
  • 2
0

Use Following Simple Function to Remove Duplicates from array using jquery

var YourArray = [  
{ label: '1', url: '1.jpg'  },

{ label: '1', url: '2.jpg'  },

{ label: '3', url: '3.jpg'  },

{ label: '4', url: '4.jpg'  },

{ label: '1', url: '5.jpg'  },

{ label: '6', url: '6.jpg'  },

{ label: '1', url: '7.jpg'  },

{ label: '8', url: '8.jpg'  },

{ label: '9', url: '9.jpg'  },

{ label: '10',url: '10.jpg' }
          ];
   var SortedArray = YourArray.filter(
                 function(a){if (!this[a.label]) {this[a.label] = 1; return a;}},
                 {}
                );
                alert(JSON.stringify(SortedArray));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Som
  • 598
  • 3
  • 12