我有一个名为items的数组,该数组具有10个网址标签值。
我有一个函数,可以从数组中随机选择5个值。
我要以URL starts with first letter w or d
I.E。我打印到控制台的每个项目都应具有url jpej names
starting with either w or 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'}];
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();
答案 0 :(得分:1)
在选择随机索引之前,先以数组filter
url
数组。一种选择是
const itemsWithWOrD = items.filter(({ url }) => /^[wd]/i.test(url));
然后,在循环中,确保使用const
,let
或var
声明变量,以避免隐式创建全局变量:
for (let index = 0; index < 5; index++) {
const randomIndex = Math.floor(Math.random() * array2.length);
const [item] = itemsWithWOrD.splice(randomIndex, 1);
console.log(item);
}
如果您不喜欢正则表达式,可以选择使用两个startsWith
:
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();
答案 1 :(得分:1)
您还可以将startLetters
放在单独的数组中,并将其传递给随机选择器函数。您还可以传递所需数量的项目,然后通过Array.filter
和Array.includes
进行过滤以获取结果:
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))