cJobject and javascript equivalents

There is plenty of reference information cJobject on this site, starting with How to use cJobject, or just enter cJobject in the search box. Here's a quick reminder of what you'll need to understand to follow this app example.

What is cJobject

It's an attempt to emulate a JavaScript object within the limits of VBA syntax. It's fully recursive, so you can have endless parents and children depth, can handle arrays, and knows how to Parse JSON, and how to create it. I use it in almost everything I do in VBA - The great thing for me is to be able to 'create classes on the fly' with dynamic properties, which is exactly what we need to be able to parameterize VBA applications. 

As a reminder here's a cJobject slide primer

Some examples

Here's how it looks against the equivalent JavaScript using a few common things you need to do. Since we are creating classes on the fly at run time, the syntax of VBA makes it a little more wordy, but it achieves approximately the same thing. There are a few useful things exposed also that don't exist in javaScript

 what  vba  JavaScript
 Create an object from a JSON string  set job = JSONParse(jsonString)  var job = JSON.parse(jsonString);
 Create a JSON string from an object  JSONStringify (job) or job.stringify()  JSON.stringify(jsonString);
 Create a new object (using Nothing as the argument makes this the root)  set job = new cJobject
job.init (Nothing)
 var job = {};
 Access the key of an object (for example the word 'name' of the object job.name)  s = job.key  n/a
 Access the full key of an object (for example the word 'boss.name' of the object job.boss.name)  s = job.fullKey  n/a
 Access the child of an object  set j = job.child("name")  j = job.name;
 Access the value of a child  v = job.child("age").value  v = job.age;
 Access the string value of a child  v = job.child("age").toString  v = job.age.toString();
 Access the value of an object  v = j.value  v = j
 Access multiple values deep  v = job.child("name.first").value or
v = job.child("name").child("first").value
 v=job.name.first
 Iterate through the children of an object  for each j in job.children  for (var k in job)
 Find out how many children an object has  n = job.children.count  var n = 0; for (var k in job) { n++ ;}
 Find the parent object   set j = job.parent  n/a
 Find an object with a particular  key somewhere in a branch  set j = job.find ("child.first")  n/a
 Add a new object as a child  job .add "someKey"  job.someKey = {};
 Add a new object and give it a value  job.add "someNumber","someValue"  job.someNumber = someValue;
 Set an object to a value  j.value = 100  j = 100;
Add an array job.add("someArray").addArray  job.someArray = [];
Add a new member value to an array  job.child("someArray").add(,200)  job.someArray.push(200);
 Add a new object to an array  job.child("someArray").add("newObject")  job.someArray.push({someObject:null});
 iterate through an array  for each j in a.children  for (var i=0; i <a.length;i++) { a[i];}
 check if an object exists  if not job.childExists("someKey") is nothing  if (job.hasOwnProperty("someKey")) 
 check to see if im part of an array  if job.isArrayMember   n/a
 find the topmost object of this tree  set root = job.root  n/a
 Access my index number in an array   n = job.childIndex  n/a
 Access the 1st element of an array   a.children(1)  a[0]
 The length of an array  a.children.count  a.length
 check to see if im an array  if a.isArrayRoot  if (Array.isArray(a)) 

Complete list of cJobject methods and properties


You can get me on Google plus, Twitter or this forum.

For help and more information join our forum,follow the blog or follow me on twitter .



Comments