1

Is it possible to loop a json object one by one ?
let's say I have this object (text). I want to loop on text and get next item every 1 minute until I get them all. How can I do this in javascript ?

 var text = 
        {
        "name":"John Johnson",
        "street":"Oslo West 16",
        "determine":"555 1234567",
        "JSON":"concernant",
        "you":"value"
        };

       setInterval(function(){
       //text.getnext
       }, 1000);

sorry if it is duplicated I have not found solution.

dmx
  • 1,862
  • 3
  • 26
  • 48
  • 2
    you are using an object. a [JSON](http://json.org/) is a string. – Nina Scholz Jul 22 '16 at 16:09
  • 2
    Note: [object property order is not guaranteed](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – gre_gor Jul 22 '16 at 16:16

4 Answers4

3

You can use Object.keys if the order is not important:

var keys = Object.keys(text), i = 0;

var myInterval = setInterval(function(){
   if(i >= keys.length) clearInterval(myInterval); // You need to clear the interval ;) 
   var current = text[keys[i++]];
  //.....
}, 1000*60); //1 minute

I hope this will help you.

Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
1

You could use the superb example of MDN for Iterators and use it for the properties.

For every interval, the iterator is called with next() and then the value is checked. if done, clear the interval or display the property.

function makeIterator(array) {
    var nextIndex = 0;
    return {
        next: function () {
            return nextIndex < array.length ?
                { value: array[nextIndex++], done: false } :
                { done: true };
        }
    };
}

var text = { "name": "John Johnson", "street": "Oslo West 16", "determine": "555 1234567", "JSON": "concernant", "you": "value" },
    iterator = makeIterator(Object.keys(text)),
    interval = setInterval(function () {
        var k = iterator.next();
        if (k.done) {
            clearInterval(interval);
        } else {
            console.log(text[k.value]);
        }                                // this value is for demo purpose
    }, 1000);                           // for a minute interval change to 60000
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Why would I go to all the trouble of making an iterator when an array (of keys in this case) is already an iterable? –  Jul 23 '16 at 08:25
  • because its a nice package and reusable without changes. and if native implemented, it could be used without workaround. – Nina Scholz Jul 23 '16 at 08:35
  • What I am saying is, you have recreated all the iterator logic which is already built into arrays, which is completely pointless. –  Jul 23 '16 at 08:39
  • i do not get the point. yes you can use an index for iteration, but that is covered in the other answers. should i delete my answer, because it's useless? or what can i do? – Nina Scholz Jul 23 '16 at 08:46
0

Native:

You can get a list of keys in an array by using Object.keys and setTimeout built-in functions.

Object.keys returns an array of keys an an object setTimeout takes a function and executes it after certain number of milliseconds (1000) in this case

So in your case

  var keys = Object.keys(array);
  function iterate(keys, index){
     if(index == keys.length)
        return;
     else{
        console.log("current is " + array[keys[index]]);
        setTimeout(function(){iterate(keys, index + 1)}, 1000);
     }
  }

Then call itterate(text, 0);

Ahmadposten
  • 267
  • 3
  • 15
0

You should first solve the generic problem of sending out the element of an array with some delay. Once you've done that, it's easy enough to apply that to the keys of an object.

Here's a simple version using setTimeout:

function emitWithDelay(array, callback) {
  var n = 0;

  function emitOne() {
    if (n >= array.length) return;

    setTimeout(function() {
      callback(array[n++]);
      emitOne();
    }, 1000);

  }

  emitOne();
}

Now you can just do

emitWithDelay(Object.keys(obj), myCallback);

Streams

This is a good problem for using streams, or observables. It is as simple as

Observable.from(Object.keys(obj))
  .delay(1000)
  .subscribe(elt => console.log("got elt", elt));

Async approach

Another idea is to use async functions, an ES7 feature, as follows:

async function emitWithDelay(array, callback) {
  for (elt of array) {
    callback(elt);
    await sleep(1000);
  }
}

Where sleep is something like

function sleep(n) {
  return new Promise(resolve => setTimeout(resolve, n));
}

As you can see, the possibilities are endless.