Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to format and use data for JSON (JavaScript Object Notation) in JavaScript

2 Answers

0 votes
function obj_size(obj)  {
    var key, count = 0;
     
    for (key in obj.worker) 
         if (obj.worker.hasOwnProperty(key)) 
             count++;
              
    return count;
}


const w = '{"worker" : [' +
'{"name":"Tom", "age":37},' +
'{"name":"Olivia", "age":42},' +
'{"name":"Jerry", "age":51},' +
'{"name":"Alessia", "age":29} ]}';
 
const JSONobj = JSON.parse(w); 
 
const size = obj_size(JSONobj);
 
for (i = 0; i < size; i++)
     console.log(JSONobj.worker[i].name + " " + JSONobj.worker[i].age);
 



 
/*
run:
 
"Tom 37"
"Olivia 42"
"Jerry 51"
"Alessia 29"
 
*/

 



answered Jun 26, 2015 by avibootz
edited May 30, 2022 by avibootz
0 votes
const w = '{"worker" : [' +
'{"name":"Tom", "age":37},' +
'{"name":"Olivia", "age":42},' +
'{"name":"Jerry", "age":51},' +
'{"name":"Alessia", "age":29} ]}';
 
const JSONobj = JSON.parse(w); 
 
const size = Object.keys(JSONobj.worker).length

for (i = 0; i < size; i++)
     console.log(JSONobj.worker[i].name + " " + JSONobj.worker[i].age);
 



 
/*
run:
 
"Tom 37"
"Olivia 42"
"Jerry 51"
"Alessia 29"
 
*/

 



answered May 30, 2022 by avibootz
...