question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello

Let’s imagine we’re having an object:

{% set object = {
    title: 'title',
    content: 'content'
} %}

Is there any way to modify it’s properties without overriding whole object?

For example, like:

{% set object.title = 'New title' %}

or to add another key:

{% set object.test = 'New key value' %}

Currently such attempts will return error TypeError: Cannot call method 'charAt' of undefined, of course.

Now I wonder, is there any workaroud for it, or it isn’t possible and won’t be ever possible too?

If I do understand it right, jinja has such possibility, with do block http://stackoverflow.com/questions/11047886/modifying-dictionary-attributes-in-jinja2

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
jan-dolejsicommented, May 14, 2018

This looks simpler and sufficient for this case:

env.addFilter('setAttribute', function(dictionary, key, value) {
            dictionary[key] = value;
            return dictionary;
        });

Usage

{% set myDict = {"key1": 0, "key2": 0, "key3": 0}%}
{% set myDict = myDict|setAttribute('key2', 123) %}
{{myDict['key2']}}

… prints “123” as expected.

2reactions
ArmorDarkscommented, May 14, 2018

@jan-dolejsi the output is correct. You’ve set key2 to 123, and it prints 123 afterward. What did you expect?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modifying Objects | Intro to JS: Drawing & Animation | Computing
Pamela shows how to modify objects while your program is running, to change existing properties or add new ones. Created by pamela ❤....
Read more >
How To Add, Modify and Delete JavaScript Object Literal ...
How To Add, Modify and Delete JavaScript Object Literal Properties · Add property to an Object: · Delete property from an Object: ·...
Read more >
Understanding Objects in JavaScript - DigitalOcean
In this tutorial, we will review how to create an object, what object properties and methods are, and how to access, add, delete,...
Read more >
Modify Objects - CustomGuide
1. Select an object. 2. Click Edit on the menu bar. 3. Select Copy.
Read more >
Modifying Object Properties - L3HarrisGeospatial.com
Modifying Object Properties ... Some IDL objects have properties associated with them; things like color, line style, size, and so on. You can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found