1

I have an attribute of type map populated with some values in a component. On click action, I want to remove certain element whose key I know. I am not able to achieve this using:

var key=component.get('v.key'); delete map[key];

Error thrown is: '[TRUE is not defined] Failing descriptor'. Here 'true' is the value for 'key' in map.

I have also tried

delete map[key];

1
  • It's suspicious that you have a map with booleans as keys, and that you're storing the key in a separate attribute. Commented Dec 12, 2016 at 14:24

2 Answers 2

3

component is not a javascript map in way we used to thinks about javascrip maps. Let get/set methods not mislead you. The attributes of components can be set to null though. The attributes are declared in cmp file. You can declare the attribute of type Object and add/delete fields in manner you describe.

2
  • Do you mean to say that lightning framework allow us to add key value pairs in Map type of attribute but not remove a key-pair? If yes then why would it do so?
    – Sarang
    Commented Dec 13, 2016 at 5:37
  • @Rangya if the attribute is a Object type, you can add/remove fields the way you like (unless locker service will get you one day). Speaking about component itself - it operates with attributes, not fields.
    – Pavlonator
    Commented Dec 13, 2016 at 17:06
2

Correct as of Summer'17

As maps in Lightning Experience are really just objects, you can write a little helper method to remove the key.

Here is an example: (I am using a map to store errors from a number of child components)

deleteError : function(component, key) {

    // retrieve errorMap attribute
    var errorMap = component.get("v.errorMap");

    // delete the key
    delete errorMap[key];

    // set the attribute with the new errorMap
    component.set("v.errorMap", errorMap);

}

Just FYI, this is my add method:

addError : function(component, key, value) {

    // retrieve errorMap attribute
    var errorMap = component.get("v.errorMap");

    // if it is null or undefined, trying to add
    // a key generates an error, so set it as an
    // empty object.
    if (!errorMap) {
        errorMap = {};
    }

    // set the key with the value
    errorMap[key] = value;

    // set the attribute with the new errorMap
    component.set("v.errorMap", errorMap);

}

I hope this helps (please up-rate if it does ... I'm trying to build reputation ;-)

Jonathan

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .