1

I have a custom label with comma seperated values: '17825,58963,01236'

import cli_message from '@salesforce/label/c.cli_message ';
        export default class sendMessage extends LightningElement {

         label = {cli_message};

        }
         connectedCallback() {
            //Object.values(this.label) returns the custom label values

          console.log(Object.values(this.label).split(',')) // returns nothing
        }
3
  • What does object.values(this.label) return? What type? – glls Oct 30 '19 at 20:41
  • It returns the custom label value.. 17825,58963,01236 – cartman Oct 31 '19 at 15:17
  • Thats not a type, anyways, answered bellow, but yeah, split wont work if its an array – glls Oct 31 '19 at 15:21
2

It's because Object.values(this.label) returns an array ( ["17825,58963,01236"] ) while the split() method applies to a string.

What you can do is:

import cli_message from '@salesforce/label/c.cli_message ';

export default class sendMessage extends LightningElement {

   label = {cli_message};


   connectedCallback() {
      window.console.log("Object.values(this.label) >>", Object.values(this.label)) //Object.values(this.label) >> ["17825,58963,01236"]
      window.console.log("test", Object.values(this.label)[0].split(',')) //Array(3) ["17825", "58963", "01236"]
   }
}

3
  • you can also use this approach window.console.log("array >> ", this.label.cli_message.split(",")) – dibocor Oct 30 '19 at 23:57
  • I just realized that it was returning the value as array and not string Object.values(this.label).toString().split(',') worked for me. – cartman Oct 31 '19 at 15:19
  • glad that works. please can you accept my answer, there is a green button for that – dibocor Oct 31 '19 at 15:33
0

You can use :

var label = [12,13,234]; 
Object.keys(label).forEach(function(k, i) {
    console.log(label[k]);
});
2
  • label isn't an array, it is an object --> label={cli_message: "17825,58963,01236"} – dibocor Oct 31 '19 at 11:08
  • also when the Object.values() method takes as parameter an array it returns an array not containing the elements but the indexes – dibocor Oct 31 '19 at 11:09

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.