generate json within loop

Asked

Viewed 73 times

2

I need some help:

I have the following json

var jsonString = "{ \"0%\" : { \"margin-left\":\"-0%\"},";
    jsonString += " \"25%\" : { \"margin-left\":\"-0%\"},";
    jsonString += " \"30%\" : { \"margin-left\":\"-100%\"},";
    jsonString += " \"50%\" : { \"margin-left\":\"-100%\"},";
    jsonString += " \"55%\" : { \"margin-left\":\"-200%\"},";
    jsonString += " \"75%\" : { \"margin-left\":\"-200%\"},";
    jsonString += " \"80%\" : { \"margin-left\":\"-300%\"},";
    jsonString += " \"100%\" : { \"margin-left\":\"-300%\"}}";

jsonString = JSON.parse(jsonString);

$.keyframe.define([
  $.extend(
      { name: 'tocaSlide' }, 
          jsonString
      )
]);

Works for my purposes.

But the way it is, I’ve got a var jsonString static.

I need it to be dinâmico.

So I’m generating him into a loop for, but it’s going wrong.

I wish I knew where I was going wrong.

  var jsonString ={};   

  for (i = 0; i < quantasImagens; i++) {    

  tMin = t + tempoTransicao;
  tMax = t + tamanhoIntervalos; 
  t+=tamanhoIntervalos;

  if(i==0) tMin=0;
  if(i==quantasImagens) tMax=100;         

  jsonString += "'" + tMin + "%' : { 'margin-left':'-" + tempoImagens + "%'],";
  jsonString += "'" + tMax + "%' : { 'margin-left':'-" + tempoImagens + "%'},";

  tempoImagens+=100;

  }


  jsonString = JSON.parse(jsonString );

1 answer

2

You can think of "mounting an object" instead of thinking of strings that should be transformed into an object...

I think what you’re looking for would be something like this:

var obj = {};

for (i = 0; i < quantasImagens; i++) {

  tMin = t + tempoTransicao;
  tMax = t + tamanhoIntervalos;
  t += tamanhoIntervalos;

  if (i == 0) tMin = 0;
  if (i == quantasImagens) tMax = 100;

  obj[tMin + '%'] = { 'margin-left': '-' + tempoImagens + '%'};
  obj[tMax + '%'] = { 'margin-left': '-' + tempoImagens + '%'};
  tempoImagens += 100;
}


console.log(obj);

Note: the condition if (i == quantasImagens) tMax = 100; will never be reached as the loop will stop when the i < quantasImagens.

  • gave the following error: Uncaught Typeerror: Cannot set Property '0%' of Undefined

  • @Carlosrocha yes, of course my mistake, had var obj = obj; and not var obj = {};.

  • I’m sorry, here I posted wrong. But in drreamweaver is var obj = obj. and it didn’t work anyway. Do you have any idea what might be? See www.hotplateprensas.com.br/styles/slide2.php

  • @Carlosrocha this site gives me 500 Internal Server Error. Put an example of quantasImagens and variables that are defined outside the code to mount a working example.

  • So, @Sergio. Good Morning! For me it opens normal here. Maybe you are putting spaces in the URL! The error that returns me is this: "Uncaught Typeerror: Cannot set Property '0%' of Undefined". This in Chrome. In Fireox, the error is "Typeerror: obj is Undefined"

  • @Carlosrocha has now opened. You have var obj = obj; in the code. Corrects for var obj = {};

  • fixed. It worked. But, as always, a logic error occurred! Can you help me? https://answall.com/questions/250778/loop-for-passando-de-100

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.