Search code examples
javascriptractivejs

How do you delete a ractivejs object


I know in vanilla JS you can just use the delete command to remove an object but I can't find anything in the docs for object manipulation.

For example how would I delete this object in ractivejs?

template.set('object', {hello: 'world', bye: 'world'});

I want to later on delete the bye object from that 'object'.


Solution

  • From the template's perspective, it usually doesn't matter if you just set the value to undefined:

    template.set( 'object.bye' );
    

    Unless you're using it in a hash, then you can delete the key and call update:

    delete template.get( 'object').bye;
    template.update( 'object.bye' );
    

    It's currently a requested feature (https://github.com/ractivejs/ractive/issues/1649) to add an unset method:

    template.unset( 'object.bye' );
    

    If you look in the comments on that issue, there's a Ractive.prototype.unset polyfill you can use.