动态地在另一个数组中添加对象

时间:2018-09-27 03:49:33

标签: javascript angular

我正在尝试将对象添加到4个不同的数组中,例如循环。当“类型”匹配时,第一个对象应转到topArray,第二个对象应转到bottomArray,第三个对象应转到rightArray,就像一个循环,并且portId也应随各自的数组而变化,例如,如果第一个对象是一循环后,转到topArray portId应该为top0,五个对象应该转到topArray,而portId应该为top1,依此类推……

这是代码。

this.comps.forEach((comp) => {
      arrayPorts = [];

        let portlist = comp.ports;
  console.log(portlist);
        portlist.forEach((po) => {
          switch (po.type) {
            case 'rest':
           let  docu1 = {
                "portId": "right0",
                "portColor": "#ff0000",
                "portname": "rest"
              }
              arrayPorts.push(docu1);
              break;
            case 'restcon':
            let  docu2 = {
                "portId": "right0",
                "portColor": "#ff8c8c",
                "portname": "restcon"
              }
              arrayPorts.push(docu2);
              break;
            case 'message':
            let  docu3 = {
                "portId": "right0",
                "portColor": "#006600",
                "portname": "message"
              }
              arrayPorts.push(docu3);
              break;
            case 'messagecon':
            let  docu4 = {
                "portId": "right0",
                "portColor": "#00b300",
                "portname": "messagecon"
              }
              arrayPorts.push(docu4);
              break;
            case 'event':
            let docu5 = {
                "portId": "right0",
                "portColor": "#0019ff",
                "portname": "event"
              }
              arrayPorts.push(docu5);
              break;
            case 'eventcon':
            let  docu6 = {
                "portId": "right0",
                "portColor": "#9ea7ff",
                "portname": "eventcon"
              }
              arrayPorts.push(docu6);
              break;
          }

        })
        this.components.push({
          key: finalindex + 1,
          name: this.reComps[comp._id].name,
          id: comp._id,
          version: "Ver: " + comp.version,
          icon: this.reComps[comp._id].icon,
          loc: "",
          group: this.num,
          topArray: arrayPorts,
          bottomArray: [],
          rightArray: [],
          leftArray: [],

        })
        finalindex++;
      }

    });

comp.ports包含对象数组

这是我需要的最终输出

{id: "abcd"
icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoA"
key: 10
loc: ""
name: "ABCD"
topArray: [{"portId":"top0", "portColor":"#ff0000", "portname":"rest"},{"portId":"top1", "portColor":"#ff0000", "portname":"message"}]
bottomArray: [{"portId":"bottom0", "portColor":"#ff8c8c", "portname":"event"},
{"portId":"bottom1", "portColor":"#ff0000", "portname":"message"}]
rightArray: [{"portId":"right0", "portColor":"#0019ff", "portname":"message"}]
leftArray: [{"portId":"left0", "portColor":"#ffff33", "portname":"rest"}]
version: "Ver: 10.1.0"}

我需要将该对象(此数组(topArray,bottomArray,rightArray,leftArray))放入循环中,我发现有点混乱。 请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,则需要将端口推入各自的阵列。这是一种方法。

const arrays = {
    left: [],
    right: [],
    top: [],
    bottom: []
};

const ports = [{
    "portId": "top0",
    "portColor": "#ff0000",
    "portname": "rest"
}, {
    "portId": "left0",
    "portColor": "#ff0000",
    "portname": "rest"
}, {
    "portId": "bottom0",
    "portColor": "#ff0000",
    "portname": "rest"
}];

ports.forEach((port) => {

    Object.keys(arrays).forEach((key) => {
        
        // Create a regular expression to match the portId
        
        const regEx = `${key}[0-9]*`;

        const arrayToPush = port.portId.match(new RegExp(regEx));
        
        // If we have found the correct array push the port there
        
        if (arrayToPush) arrays[key].push(port);
    });
});

console.log(arrays);