Hey I'm struggling and was hoping that maybe someone here could help. I'm making a web app that uses a big array of objects that looks similar to this:
var arr = [
{
prop1 : "foo",
prop2 : ["foo1", "foo2", "foo3", "foo4"],
prop3 : "foo5",
prop4 : "foo6",
},
{
prop1 : "bar",
prop2 : ["bar1", "bar2", "bar3", "bar4"],
prop3 : "bar5",
prop4 : "bar6",
},
{
prop1 : "something",
prop2 : ["something1", "something2", "something3", "something4"],
prop3 : "something5",
prop4 : "something6",
}
]
The array is much bigger than that but you get the idea. I want to display only 5 objects from the array at random each time the code is run which I did like this:
randomArr = arr.sort(() => Math.random() - .5);
spliceArr = randomArr.splice(5);
It works, but it sometimes shows objects from the main array multiple times. How can I write the code so the it will return objects from the array only once?
sort()modifies the array in place, so the assignment torandomArris superfluous.