• Structured data is a bit more of a pain - you are stuck doing reasonably low level work to get it stored efficiently.

    The easiest way to actually access the data is to create an ArrayBuffer (byte array) of the length required and to use DataView to access it: http://www.espruino.com/Reference#DataVi­ew

    If you were particularly interested in making it 'nice' you could use getters and setters:

    var ELEMENT_SIZE = 3;
    
    class Element {
      constructor(buf, idx) {
        this.d = new DataView(buf,idx*ELEMENT_SIZE,ELEMENT_SI­ZE);
      }
      
      get a() { return this.d.getInt16(0); }
      set a(v) { this.d.setInt16(0, v); }
      get b() { return this.d.getUint8(2); }
      set b(v) { this.d.setUint8(2, v); }
    };
    
    
    var buf = new ArrayBuffer(5*ELEMENT_SIZE);
    
    function el(idx) {
      return new Element(buf, idx);
    }
    
    el(0).a = 257;
    el(3).b = 42;
    print(el(3).b);
    

    But it's not going to be especially fast if you care about that, since it's creating a new object each time you call el(..)

About

Avatar for Gordon @Gordon started