Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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/';
}

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda