【问题标题】:Create an object based on items in a list - javascript基于列表中的项目创建对象 - javascript
【发布时间】:2023-01-08 03:52:14
【问题描述】:

this.items 是从数据库中读取的项目列表。对于列表中的每个项目,我想创建 Shape.Rect 对象并将它们加载到名为 stencil.load 的函数中。此功能无法动态添加项目,因此我需要创建所有项目并同时加载它们。

我试过执行forEach,如下面的代码示例所示,但它只注册了列表中的最后一项。知道该怎么办吗?

.
.
.

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');

});

.
.
.

【问题讨论】:

    标签: javascript arrays javascript-objects antv


    【解决方案1】:

    没有看过stencil.load API,但根据你的描述和用法,我认为你可以先将所有项目映射到Shape.Rect,然后再加载它们。

    例如

    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');
    

    【讨论】:

    • 非常感谢!在职的 :)
    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多