for (let key in data) loop 1 or more times with javascript -


i want loop 1 time or want loop specified times in loop.

i have big json , structure this:

{   "-item1" : {     "added_by" : "admin@gmail.com",     "id" : "-klxuo5hgr7sdesgeea1",     "name" : "apivita"   },   "-item2" : {     "added_by" : "admin@gmail.com",     "id" : "-klxuq4sgtfefuab2xh1",     "name" : "lierac"   },   "-item3" : {     "added_by" : "admin@gmail.com",     "id" : "-klxus1i3p7z2ydftqph",     "name" : "bioderma"   } } 

how loop 1 time?

// object key for(var key in data) {   var value = data[key]; } 

i want item1 object data. or first 5 items easy ?

you can add counter , exit loop if counter has reached limit:

var counter = 0;  var limit = 3;  var data = [1, 2, 3, 4, 5, 6, 7];  (var key in data) {    var value = data[key];    console.log('key: ' + key + ' --> value: ' + value);    if (++counter >= limit) break;  }


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -