'Store array in JSON
I was trying store an array into JSON. For the JSON script, is it correct to write it just like this? {"bgArr" : ["bg1","bg2","bg3"]}
Might need some correction. I doing this to make the splice function to be fully work.
var promoCode = '';
var bg1 = 'https://cdn.pixabay.com/photo/2020/06/01/22/23/eye-5248678__340.jpg';
var bg2 = 'http://farm5.static.flickr.com/4017/4717107886_dcc1270a65_b.jpg';
var bg3 = 'http://images6.fanpop.com/image/photos/41500000/adorable-puppies-cute-puppies-41538743-590-393.jpg';
var JsonObject, bgArr;
// JS array
var bgArr = [bg1, bg2, bg3];
// Convert (js array) to (json object)
JsonObject = JSON.parse(JSON.stringify(bgArr));
// type
console.log(typeof(JsonObject));
// JsonObject
console.log(JsonObject);
for(var i = bgArr.length-1;i>=0;i--){
selectBG = bgArr.splice(Math.floor(Math.random()*bgArr.length), 1)[0];
console.log(selectBG);
}
if (selectBG === bg1) {
promoCode = 'SCRATCH400';
} else if (selectBG === bg2) {
promoCode = 'SCRATCH500';
} else if (selectBG === bg3) {
promoCode = 'SCRATCH600';
} else if (bgArr.length === 0) {
alert("No more voucher, back to main website");
window.location = 'https://www.google.com/';
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
