Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create an object based on items in a list – javascript

this.items is a list of items read from a database. For each item in the list I want to create the Shape.Rect-object and load them into a function called stencil.load. This function cannot add items dynamically, so I need to create all the items and load them simultaneously.

I’ve tried doing a forEach as shown in the codesample below, but it only registers the last item from the list. Any idea what to do?

.
.
.

this.items.forEach((item) => {

  const result = new Shape.Rect({
    shape: 'rect',
    label: item.component_name,
    id: item.component_id,
    x: 5,
    y: 50,
    width: 100,
    height: 25,
  });

  stencil.load([result], 'group1');

});

.
.
.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Haven’t looked at the stencil.load API, but from what you described and the usage I think you can just map all items to Shape.Rect first then load them all.

e.g.

const rects = this.items.map(item => new Shape.Rect({
    shape: 'rect',
    label: item.component_name,
    id: item.component_id,
    x: 5,
    y: 50,
    width: 100,
    height: 25,
}));

stencil.load(rects, 'group1');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading