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];

  • It's suspicious that you have a map with booleans as keys, and that you're storing the key in a separate attribute. – citizen conn Dec 12 '16 at 14:24
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.

  • 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 Dec 13 '16 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 Dec 13 '16 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.