1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
const artDataArray = [ { "imageUrl": "images/img1.jpg", "tags": "知更鸟、湖蓝色、十分可爱、皮克斯渲染" }, { "imageUrl": "images/img2.jpg", "tags": "知更鸟、个性的眉毛、模糊毛皮" }, { "imageUrl": "images/img3.jpg", "tags": "知更鸟、剪纸风格、个性的眉毛" }, { "imageUrl": "images/img4.jpg", "tags": "知更鸟、油画、十分可爱、特殊的羽毛" }, { "imageUrl": "images/img5.jpg", "tags": "男性、卡通、书、桌子" }, { "imageUrl": "images/img6.jpg", "tags": "男性、卡通、玩具、眼镜" }, { "imageUrl": "images/img7.jpg", "tags": "男性、卡通、玩具" }, { "imageUrl": "images/img8.jpg", "tags": "男性、卡通、书" }, { "imageUrl": "images/img9.jpg", "tags": "沙滩、遮阳伞、散步" }, { "imageUrl": "images/img10.jpg", "tags": "沙滩、椰子树、一群人" }, { "imageUrl": "images/img11.jpg", "tags": "沙滩、遮阳伞、人、包" }, { "imageUrl": "images/img12.jpg", "tags": "沙滩、回忆、相框" } ]
function generateAndDisplayImages(imageCount, selectedText) { let imgAry = [] let arr = [] for (let i = 0; i < artDataArray.length; i++) { const keyword = artDataArray[i].tags.split("、") arr[i] = { count: 0, data: artDataArray[i] } for (let j = 0; j < keyword.length; j++) { if(selectedText.indexOf(keyword[j]) != -1) arr[i].count++ } } const sortArr = arr.sort((a,b)=> b.count - a.count).slice(0,imageCount) sortArr.map(item => imgAry.push(item.data)) return imgAry; }
|