ObservableArray: add a new column

Posted by: lucas.gagneten on 29 April 2019, 4:38 pm EST

    • Post Options:
    • Link

    Posted 29 April 2019, 4:38 pm EST

    Hello,

    I have an ```

    ObservableArray

    [

    0: {time: 16:22:30 , y0: 74.60}

    1: {time: 16:23:42, y0: 74.89}

    2: {time: 16:24:54 , y0: 75.52}

    3: {time: 16:26:06, y0: 75.46}

    …]

    
    I need to add a new column, like this:
    

    [

    0: {y1: 55.71}

    1: {y1: 55.45}

    2: {y1: 54.28}

    3: {y1: 54.15}

    …]

    
    So, the result should be:
    

    [

    0: {time: 16:22:30 , y0: 74.60 , y1: 55.71}

    1: {time: 16:23:42, y0: 74.89 , y1: 55.45}

    2: {time: 16:24:54 , y0: 75.52 , y1: 54.28}

    3: {time: 16:26:06, y0: 75.46, y1: 54.15}

    …]

    
    How can I add the column "y1" to the array?
    Thanks
  • Posted 29 April 2019, 9:23 pm EST

    Hello!

    The ObservableArray really is a JavaScript array, so you can add properties to the objects in the ObservableArray as you would with a regular array. For example:

    arr.forEach((item, index) => {
      item.y1 = index * 3;
    });
    
    

    Or, if you don’t like the forEach command:

    for (let i = 0; i < arr.length; i++) {
      arr[i].y1 = i * 3;
    }
    
    

    If you have controls bound to the array and want them to be notified of the change, you can wrap either of these in a call to deferUpdate:

    arr.deferUpdate(() => {
      arr.forEach((item, index) => {
        item.y1 = index * 3;
      });
    });
    
    

    I hope this helps.

  • Posted 30 April 2019, 10:29 am EST

    Thanks!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels