I am trying to loop through an array of objects, which depending on their type property, will create a different class and append it to an array. The problem is that the output is always just a list of duplicates of the last class created.
// Create Elements from Content
// The id's are created by UUIDV4 and are all different.
self._elements = new Array
let e;
self.content.page_data.forEach(cont => {
switch (cont.type) {
case 'paragraph':
e = new Paragraph()
console.log(e.element.id)
self._elements.push(e)
break;
case 'title':
console.log('title')
return
}
})
console.log(self._elements)
After troubleshooting I've found that the problem isn't e
, as each instance is different, however once it is pushed / added to the array, the problem occurs. The problem only occurs when instances of Paragraph()
are created. As other items in the array, like text, will remain the same while still duplicating the last class.
Please can someone explain what I'm missing here?
EDIT - Class for Paragraph
class Paragraph {
constructor(value = '') {
self._element = template_paragraph.cloneNode(true).content.children[0];
const quil = self._element.children[0].children[1].children[0];
self.quill = new Quill(quil, {
modules: {
toolbar: [
[{ header: [1, 2, false] }],
['bold', 'italic', 'underline'],
[{ list: 'ordered' }, { list: 'bullet' }]
]
},
placeholder: 'Compose an epic...',
theme: 'snow' // or 'bubble'
})
self._element.id = uuidv4()
}
get element() {
return self._element
}
set_content(content) {
// Set quill value
if (!content) return
//self.quill.setContents(content)
}
}
The quill interacts with my html clone as intended. I hope this will help.