Discord
Login
Community
DARK THEME

How can I get the set of values in an object?

So basically the length.

There are two methods.

  1. You can refer to the object property in JavaScript which will tell you how many keys there are in a given object. (getObjectlength function).

  2. You can count these fields yourself by iterating over the object. In MicroScript, just like in JavaScript, an object is something like an associative list - a given key has a value associated with it. And since it's a list, it means that you can iterate over it using a for loop.

file js code >>

// javascript 



const getObjectLength = function(obj) {
  if (obj && typeof obj === 'object') {
    return Object.keys(obj).length;
  }
  throw new Error('Provided argument is not an object');
};

global.getObjectLength = getObjectLength;

file >> main

detect = function( field, name, path, deep )
  if deep >9 then return end
//  print( '>>> Detect >>> '+path+' '+ deep)
 // print( 'field =' + field + ' field.type='+field.type+ ' name='+name )
  if field.type == 'number' then
    printNumber( field, name, path, deep )
  elsif field.type == 'string' then 
    printString( field, name, path, deep )
  elsif field.type == 'list' then 
    printList( field, name, path, deep )
  elsif field.type == 'object' then 
    print( ' Detec object >>>> ')
    printObject( field, name, path, deep )
  elsif field.type == 'function' then
    detectFunction( field, name, path, deep )
  elsif field.type == 'class' then 
    print( 'clas class class')
  end
 // print( '<<< Exit detect '+ deep)
end

detectFunction = function( fun, name, path, deep )
  if not fun.print then 
    print(' printFunction( ' + name + ' '+fun.print+ ')<<<<<<<<<<<<<<<<')
    fun.print = true  
    local deeps = ''
    for i=1 to deep  
      deeps += ' '
    end
    path = path + '.' + name
    for field in fun
      print( deeps + ' Function -----------------')
      print( deeps + path +'.' + field +' = ' + fun[field] + ' fun[field].type = ' + fun[field].type )
    end
  else
    print('rewisit')
  end
end

printFunction = function( fun, name, path, deep )
  if not fun.print then 
    print(' printFunction( ' + name + ' '+fun.print+ ')<<<<<<<<<<<<<<<<')
    fun.print = true  
    local deeps = ''
    for i=1 to deep  
      deeps += ' '
    end
    path = path + '.' + name
    printList( fun.num_args, 'num_arg', path, deep+1 ) // number
    printList( fun.ops, 'ops', path, deep+1 ) //list
    printList( fun.opcodes, 'opcodes', path, deep+1 ) //list
    printList( fun.arg1, 'arg1', path, deep+1 ) //list
    printList( fun.ref, 'ref', path, deep+1 ) //list

    printList( fun.import_refs, 'import_refs', path, deep+1 ) //list
    printList( fun.import_values, 'import_values', path, deep+1 ) //list
  
  else
    print('rewisit')
  end
end

detectList = function( list, name, path, deep )
  if not list.print then 
    local deeps = ''
  for i=1 to deep  
    deeps += ' '
  end
    list.print = true
    path += '.' + name
    print( deeps + 'printList <<<<<<<<<<<<<<<<<<')
    print( deeps + path + ' is List, length=' + list.length )
    for i = 0 to list.length-1 by 1
      item = list[ i ]
      detect( item, name+'['+i+']', path, deep + 1 )
    end
    print( 'end PrintList <<,<<<<<< ')
  end
end

printList = function( list, name, path, deep )
  local string = path +'.' + name +' = ['
  for item in list
    string += ', ' + item 
  end
  string += ']'
  print( string )
end

printObject = function( obj, name, path, deep )
  if not obj.print then 
    print( path + "." + name +" :' is object >>>> ')")
    local deeps = ''
    for i=1 to deep  
      deeps += ' '
    end
    obj.print = true
    for field in obj 
      detect( obj[field], field, path + "." + name, deep + 1 )
    end
    print( path + "." + name +" :' end object <<< ')")
  end
end

printString = function( string, name, path, deep )
  print( path + '.' + name + '=' + string + ' is string' )
end

printNumber = function( number, name, path, deep )
  print( path +'.'+ name + '=' + number + ' is number')
end

ObjectLength = function( obj )
  local count = 0
  for field in obj 
    count += 1
  end
  return count
end  
  

TestClass = class
  constructor = function()
     a = 100
     b = 200
     str = "abcdef"
  end
  
  fun1 = function(x,y,z)
  end
end

init = function()
  test = object
    x = 123
    y = 456
    name = "test object"
  end
  
  testClass = new TestClass()
  
  print( "The object has " + ObjectLength( test ) + " fields") 
  print( "The object has " + getObjectLength( test ) + " fields") 


  detect( test, "test", "", 2 )

 // detect( testClass, "testClass", "", 2 )
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community