簡體   English   中英

動態地在另一個數組中添加對象

[英]adding a Object in different array dynamically

我試圖將一個對象添加到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)的循環中推送對象,我發現有點混亂。 請幫我解決這個問題。

如果我正確理解了您的問題,則需要將端口推入各自的陣列。 這是做到這一點的一種方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM