Reputation: 205
I have an array named items that has 10 url label values.
I have a function which selects 5 values randomly from the array.
I want to make the selection in such a way that the URL starts with first letter w or d
I.E. each item I print to the console should have the url jpej names
starting with either w or d
How can I achieve this?
var array2 = [];
var items = [
{label: 'crow',
url: 'wcrow.jpg'},
{label: 'donkey',
url: 'ddonkey.jpg'},
{label: 'lion',
url: 'wlion.jpg'},
{label: 'dcat',
url: 'dcat.jpg'},
{label: 'cheetah',
url: 'wcheetah.jpg'},
{label: 'cow',
url: 'dcow.jpg'},
{label: 'dog',
url: 'ddog.jpg'},
{label: 'horse',
url: 'dhorse.jpg'},
{label: 'rabbit',
url: 'drabbit.jpg'},
{label: 'elephant',
url: 'welephant.jpg'},
{label: 'rose',},
{label: 'lotus',
url: 'flotus.jpg'},
{label: 'jasmine',
url: 'fjasmine.jpg'},
{label: 'snake',
url: 'rsnake.jpg'},
{label: 'crocodile',
url: 'rcrocodile.jpg'}];
array2 = items.slice();
function nameselect() {
for (var index = 0; index < 5; index++) {
randomIndex = Math.floor(Math.random() * array2.length)
item = array2[randomIndex];
array2.splice(randomIndex, 1);
console.log(item);
}
}
nameselect();
Upvotes: 2
Views: 53
Reputation: 18525
You could also have your startLetters
in an separate array and pass it to your random picker function. You could also pass the number of items you want and then do the filtering via Array.filter
and Array.includes
to get your results:
var items = [{ label: 'crow', url: 'wcrow.jpg' }, { label: 'donkey', url: 'ddonkey.jpg' }, { label: 'lion', url: 'wlion.jpg' }, { label: 'dcat', url: 'dcat.jpg' }, { label: 'cheetah', url: 'wcheetah.jpg' }, { label: 'cow', url: 'dcow.jpg' }, { label: 'dog', url: 'ddog.jpg' }, { label: 'horse', url: 'dhorse.jpg' }, { label: 'rabbit', url: 'drabbit.jpg' }, { label: 'elephant', url: 'welephant.jpg' }, { label: 'rose', }, { label: 'lotus', url: 'flotus.jpg' }, { label: 'jasmine', url: 'fjasmine.jpg' }, { label: 'snake', url: 'rsnake.jpg' }, { label: 'crocodile', url: 'rcrocodile.jpg' } ];
const getRandomUrls = (arr, startLetters, num) => {
let urls = items.filter(({url}) => url && startLetters.includes(url[0]));
return [...Array(num)].map(x => urls[Math.floor(Math.random()*urls.length)])
}
console.log(getRandomUrls(items, ['w', 'd'], 5))
Upvotes: 1
Reputation: 371019
First filter
the array by its url
before selecting the random index. One option is
const itemsWithWOrD = items.filter(({ url }) => /^[wd]/i.test(url));
Then, in the loop, make sure to declare your variables with const
, let
, or var
to avoid implicitly creating a global variable:
for (let index = 0; index < 5; index++) {
const randomIndex = Math.floor(Math.random() * array2.length);
const [item] = itemsWithWOrD.splice(randomIndex, 1);
console.log(item);
}
If you don't like regular expressions, an alternative is to use two startsWith
s:
const itemsWithWOrD = items.filter(({ url }) => url && (url.startsWith('w') || url.startsWith('d')));
var array2 = [];
var items = [{
label: 'crow',
url: 'wcrow.jpg'
},
{
label: 'donkey',
url: 'ddonkey.jpg'
},
{
label: 'lion',
url: 'wlion.jpg'
},
{
label: 'dcat',
url: 'dcat.jpg'
},
{
label: 'cheetah',
url: 'wcheetah.jpg'
},
{
label: 'cow',
url: 'dcow.jpg'
},
{
label: 'dog',
url: 'ddog.jpg'
},
{
label: 'horse',
url: 'dhorse.jpg'
},
{
label: 'rabbit',
url: 'drabbit.jpg'
},
{
label: 'elephant',
url: 'welephant.jpg'
},
{
label: 'rose',
},
{
label: 'lotus',
url: 'flotus.jpg'
},
{
label: 'jasmine',
url: 'fjasmine.jpg'
},
{
label: 'snake',
url: 'rsnake.jpg'
},
{
label: 'crocodile',
url: 'rcrocodile.jpg'
}
];
function nameselect() {
const itemsWithWOrD = items.filter(({ url }) => /^[wd]/i.test(url));
for (let index = 0; index < 5; index++) {
const randomIndex = Math.floor(Math.random() * array2.length);
const [item] = itemsWithWOrD.splice(randomIndex, 1);
console.log(item);
}
}
nameselect();
Upvotes: 1