Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

Remove random HTML images from a Javascript variable

I am new to Javascript so I apologize if I am using the wrong terms. I have a code where I need to show some HTML images, stored as variables containing multiple items in specific positions (defined by the class) such as:

 var fruit = ["<img class='item1' src='" + Banana + "'>" + 
        "<img class='item2' src='" + Apple + "'>" + 
        "<img class='item3' src='" + Grapes + "'>" + 
        "<img class='item4' src='" + Banana + "'>" +
        "<img class='item5' src='" + Grapes + "'>" + 
        "<img class='item6' src='" + Pear + "'>"],

Later, I need to use all of these items in a bigger picture called stimulus, e.g.:

stimulus: "<div class='container'> <img class='background' src='" + Background + "'>" + fruit + "</div>",

But then I need to only show 3 random fruits among the 6 listed in fruit. For instance, I would need to get something like:

stimulus: "<div class='container'> <img class='background' src='" + Background + "'>" + half_fruit + "</div>",

Where: var half_fruit = ["<img class='item1' src='" + Banana + "'>" + "<img class='item3' src='" + Grapes + "'>" + "<img class='item6' src='" + Pear + "'>"],

Is there any way I can do this, even if my images are not technically items of an array? I have specific fruit combinations (not just the one in the example, roughly 30 in total) and I need to remove 3 items of the ones in that specific combination while also leaving the others in the same position as they were before. This is why I am not storing images as strings in an array. Those variables are defined as var Banana = gorilla.stimuliURL('Banana.png'); etc. (Gorilla is a platform for creating experiments; the stimuliURL function calls image addresses from an online repository). Thank you for your time!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to your comments, I rearranged the variables as follows. First, I created variables for each image to access their addresses from Gorilla, e.g.

var banana_img = gorilla.stimuliURL('banana.png');

Then, I created variables for each fruit in each position:

var banana = ["<img class='item1' src='" + banana_img + "'>", "<img class='item2' src='" + banana_img+ "'>", "<img class='item3' src='" + banana_img+ "'>", "<img class='item4' src='" + banana_img + "'>", "<img class='item5' src='" + banana_img + "'>", "<img class='item6' src='" + banana_img + "'>"];

Then, I saved each fruit combination as a variable, e.g.

var fruit = [banana[0], apple[1], grapes[2], banana[3], grapes[4], pear[5]]

FInally, I sorted it and selected the first 3 to get only three random items:

half_fruit = fruit.sort( function() { return 0.5 - Math.random() } ).slice(0, 3).join(" ");

Answer or comment below if you find a better way to do this!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...