So I'm trying to build a chart as a webcomponent and I'm having some trouble with mapping an array. The error says: this.values.map is not a function
Here is the code:
import {LitElement, html, css} from 'lit-element';
class ApexChart extends LitElement {
static get properties(){
return{
values: {Array},
labels: {Array}
};
}
constructor(){
super();
this.values = [];
this.labels =[];
}
static get styles(){
return css `
`;
}
render(){
return html`
<p>${this.values.map(value => {value + 1})}</p>
`;
}
}
customElements.define('apex-chart', ApexChart);
I'm passing the values from html
<apex-chart values="[1,2,3,4]" labels="['Hi', 'Hello', 'Oi', 'Hola']"></apex-chart>
I can't see what I'm doing wrong
You have two issues:
1) the properties
type converter is not defined correctly. It should be:
static get properties(){
return{
values: { type: Array },
labels: { type: Array }
};
}
2) The map method isn't working correctly. Currently it returns undefined
values because if you use {}
you must use the return
keyword.
> [0, 1].map(value => { value + 1 })
<- (2) [undefined, undefined]
Instead use:
render(){
return html`
<p>${this.values.map(value => value + 1)}</p>
`;
}
Or:
render(){
return html`
<p>${this.values.map(value => { return value + 1; })}</p>
`;
}